<?php
class TagseditController extends AppController {

	public $name = 'tagsedit';
	public $components = array('session','acl');	
	public $_psptags = null;	
	public $_filedlistcode = null;		
	public function beforeFilter() {
	    parent::beforeFilter(); 
		$this->C_auth->autoRedirect = false; //For override login
	    $this->C_auth->allowedActions = array('divstart','viewmanageedit','viewmanageeditpost','viewmanagehtmledit','viewmanageactiveedit','viewmanageactiveeditpost','viewmanagetableedit','viewmanagecacheedit','viewmanagetableuseredit','viewmanagelabeledit','icondataeditpost','icondataaddpost','viewmanageadplaceedit','icondatalist','icondataadd','icondataedit','icondatadel');
        header('Content-Type: text/html; charset='.Param::read('App.encoding'));		
	}
	
	public function divstart() {
		if($this->C_session->check('TagsViewStartOrStop')) {
		    $this->C_session->delete('TagsViewStartOrStop');	   
		}else {
		    $this->C_session->write('TagsViewStartOrStop', 1);	   
		}
        $this->flash("执行成功", Request::referer());	
	}
	
	public function viewmanageedit() {

 		$iconname = Request::$query['iconname'];
		
		$pagename = Request::$query['pagename'];	
			
		$strdata = Request::$query['data'];
		
		$weburl = Request::$query['weburl'];
		
		$webroot = Request::$query['webroot'];
		
		$act = Request::$query['act'];	
		
		$app = Request::$query['app'];
		
		$compeld = Request::$query['compeld'];		
		
		$compeld= $this->_urldecode($compeld);
		$compeld1 = $compeld;				
		$compeld= $this->_urlencode($compeld);


		$pagename= $this->_urldecode($pagename);
		$pagename1 = $pagename;				
		$pagename= $this->_urlencode($pagename);

		$webroot= $this->_urldecode($webroot);
		$webroot1 = $webroot;				
		$webroot= $this->_urlencode($webroot);		
	
		$weburl= $this->_urldecode($weburl);
		$fromurl = $weburl;				
		$weburl= $this->_urlencode($weburl);
					
		$divwidth = Request::$query['divwidth'];
				
		$divheight = Request::$query['divheight'];
		
		$strtempdata = $strdata;
		$urlstr="&weburl={$weburl}&compeld={$compeld}&app={$app}&act={$act}&webroot={$webroot}&iconname={$iconname}&pagename={$pagename}&data={$strdata}&divwidth={$divwidth}&divheight={$divheight}";

		$strdata = $this->filtrate($strdata);	

		$this->icon_to_array($strdata);
	    $data['config'] = $this->icon_to_array;
		$data['iconcontent'] = '<psp:tags '.$strdata.' />';
		$data['iconname'] = $iconname;	
			
	    $tablearray = array();

		$DbConfig = 'default';
    		
	    $db =& ConnectionManager::getDataSource($DbConfig);
		$DbPrefix = $db->config['prefix'];		
		
	    $temparray = $db->listSources();
		foreach($temparray as $key => $value)
		{
			if($value)
			{
		        $temp_str = str_replace($DbPrefix,'',$value);
				$temp_array = $db->TableDescribe($value);
 		        $tablearray[$value][$value] = $temp_str; 

		        foreach($temp_array as $key1=>$value1)
				{
			     $tablearray[$value][$value.'.'.$value1] = $temp_str.'.'.$value1;

			    }
			}

		}

        $cachearray = array();

		
        $htmlnamearray = array();
        $temparray = array();
        $temparray1 = array();
		
        $temparray = $this->get_all_dirname($webroot1 . DS .'tags');
        $temparray1 = include($webroot1 . DS .'tags' . DS . 'tags.php');
		foreach($temparray1 as $key => $value)
		{

		   $htmlnamearray[$key] = $key.'.html';
		}

	    foreach($temparray as $key => $value)
		{
		   if(strpos($value,'.html'))
		   {
		    $htmlnamearray[str_replace('.html','',$value)] = $value;
		   }
		}

      
        $icondataarray = array();
        $temparray = array();
        $temparray = $this->get_all_dirname($webroot1 . DS .'tags' . DS . 'tempdata');
	    foreach($temparray as $key => $value)
		{
		   if(strpos($value,'.tags')) {
		    $icondataarray[str_replace('.tags','',$value)] = $value;
		   }
		}

        $this->set('icondataarray', $icondataarray);
        $this->set('htmlnamearray', $htmlnamearray);
        $this->set('cachearray', $cachearray);

        $this->set('tablearray', $tablearray);

	    $this->set('data', $data);
	    $this->set('weburl', $weburl);
	    $this->set('fromurl', $fromurl);	
							
	    $this->set('urlstr', $urlstr);
	    $this->set('DbPrefix', $DbPrefix);		
		
	    $this->set('compeld', $compeld);
	    $this->set('webroot', $webroot);
	    $this->set('app', $app);
	    $this->set('act', $act);
	
        $this->set('pagename', $pagename);			
        $this->set('strtempdata', $strtempdata);	
		
	    
	}	

    function viewmanageeditpost()
    {

		 $pagename = $this->_urldecode(Request::$data['pagename']);
		 
		 $strtempdata = Request::$data['strtempdata'];
		 
		 $fromurl = $this->_urldecode(Request::$data['fromurl']); 
		 
		 $compeld = $this->_urldecode(Request::$data['compeld']); 

		 $webroot = $this->_urldecode(Request::$data['webroot']); 
		 


		 $app = Request::$data['app'];		 
		 $act = Request::$data['act'];		 
		 		 	
		 $strdata = $this->filtrate($strtempdata);	 
	     $data = Request::$data;

         $data['config'] = filterEmptyVar($data['config']);

		if(empty($data['iconname'])) {
             exit('String iconname is NULL');
		}

		if($data['get']['iconsettype']=='1')
		{
			   $icon_str =str_replace(array('<psp:tags','/>'),array('',''),$data['iconcontent']);
               $this->icon_to_array($icon_str);
			   $data['config'] = $this->icon_to_array;
               $data['config']['iconname'] = $data['iconname'];			   
		}
		else
		{	$data['iconcontent']='';	
             $data['config']['iconname'] = $data['iconname'];
		     foreach($data['config'] as $key => $value)
		     {
			 $data['iconcontent'] .= '  '.$key."=\'".$value."\'";
		     }			 	  	
		}
               $data['iconcontent'] = str_replace(array('<psp:tags','/>'),array('',''),$data['iconcontent']);		
		       $data['iconcontent'] = str_replace(array('&quot;','&#39;','\\'),array('"',"'",''),$data['iconcontent']);			

		$filename = $webroot . DS .$pagename;
		$savefilename = $compeld;
		$savedir = str_replace($pagename ,''  ,$savefilename);

		$tempfilename = '';				
		if(is_file($savefilename) && @ filemtime($savefilename) > @ filemtime($filename))
		{			
		     $tempfilename = $savefilename;				   			
		}
		else
		{
		     $tempfilename = $filename;					
		}
		
		$fileContent = "";
		if (!($fileContent = file_get_contents($tempfilename))) 
		{
				return false;
		}
				
		$fileContent = preg_replace('/^(\xef\xbb\xbf)/', '', $fileContent); //EFBBBF
		$fileContent = str_replace($strdata, $data['iconcontent'], $fileContent);
		
		if (!file_exists($savefilename)) {

                $CviewFileName = str_replace(array('\\','/'), DS ,$savefilename);
				$temp = explode(DS, $CviewFileName);
				array_pop( $temp );
                $tempFileDir = implode( DS.'' , $temp );
				get_dir($tempFileDir);
		}
		
		if (!file_put_contents($savefilename, $fileContent)) {
			return false;
		}
        //$this->flash("执行成功", $this->_urldecode($fromurl));		 
		echo '<script >alert("修改成功!");parent.location.reload();</script>';
		exit;	
    }	
	
	
    function viewmanagehtmledit()
    {	
        if(!isset(Request::$data['dopost']))Request::$data['dopost']='';
	    $dopost = Request::$data['dopost'];	   
	   
	    if($dopost=='post')
	    {
		    $pagename = $this->_urldecode(Request::$data['pagename']);
		 
		    $strtempdata = Request::$data['strtempdata'];
		 
		    $fromurl = $this->_urldecode(Request::$data['fromurl']); 
		 
		    $compeld = $this->_urldecode(Request::$data['compeld']); 

		    $webroot = $this->_urldecode(Request::$data['webroot']); 
		 


		    $app = Request::$data['app'];		 
		    $act = Request::$data['act'];			
   
	 
	        $tplpart_1 = Request::$data['tplpart_1'];		 

	        $mokuai = Request::$data['mokuai'];
	        $filename = Request::$data['filename'];

		 
		    $strdata = $this->filtrate($strtempdata);	 
            $this->icon_to_array($strdata);
	        $old_icondata = $this->icon_to_array;


		 	
            if($mokuai==1)
		   {
  		        $temp_html_name = filterEmptyVar(explode(".",$old_icondata['htmlname']));
		        $old_icondata['htmlname'] = $temp_html_name[0];			
		        $icon_dir = $webroot . DS . 'tags';
			    if($filename)
			    {
		            $iconfile = $filename.'.html';
		            $iconfilename = $filename.'';
		            $old_icondata['htmlname'] = $iconfilename;					
			    }else{
		            $iconfile = $old_icondata['htmlname'].'.'.time().'.html';
		            $iconfilename = $old_icondata['htmlname'].'.'.time().'';
		            $old_icondata['htmlname'] = $iconfilename;					
			    }
		
	            $temparray = $this->get_all_dirname($icon_dir);
                $is = 1;
	            foreach($temparray as $key => $value)
		        {
		            if($value==$iconfile)
		            {
                        $is = 0;break;
		            }	
                }
			
		        if($is)
		        {
                    $iconhtml = stripslashes($tplpart_1);
		            $iconhtml = str_replace(array('&quot;','&#39;'),array('"',"'"),$iconhtml);				
                    $iconhtmlarray = @include($icon_dir . DS .'tags.php');
		            $iconhtmlarray[$iconfilename]['name'] =$iconfilename;
                    $iconhtmlarray[$iconfilename]['html'] =$iconhtml;
				    $iconhtmlarray[$iconfilename]['config'] =array();
					if (!(file_put_contents($icon_dir . DS .'tags.php', "<?php \nreturn " . var_export($iconhtmlarray , true) . ";\n?>")
					     && file_put_contents($icon_dir.DS.$iconfile,$iconhtml))
						) {
			            return false;
		            }				
		
		        } else {		
		            echo '<script >alert("Icondata名称有相同，请重新提交!");parent.location.reload();</script>';
		            exit;					
		        }
		     
		    }else if ($mokuai==0) {

		        $icon_dir = $webroot . DS . 'tags';
		        $iconfile = $old_icondata['htmlname'].'.html';
		        $iconfilename = $old_icondata['htmlname'];		 
                $iconhtml = stripslashes($tplpart_1);
		        $iconhtml = str_replace(array('&quot;','&#39;'),array('"',"'"),$iconhtml);				
                $iconhtmlarray = include($icon_dir . DS .'tags.php');
		        $iconhtmlarray[$iconfilename]['name'] =$iconfilename;
                $iconhtmlarray[$iconfilename]['html'] =$iconhtml;
				$iconhtmlarray[$iconfilename]['config'] =array();
				
				if (!(file_put_contents($icon_dir . DS .'tags.php', "<?php \nreturn " . var_export($iconhtmlarray , true) . ";\n?>")
					     && file_put_contents($icon_dir.DS.$iconfile,$iconhtml))
				) {
			        return false;
		        }				
		 
		    }

            $iconcontent = '';
		    foreach($old_icondata as $key => $value)
		    {
		        $iconcontent .= '  '.$key."=\'".$value."\'";
		    }			 	  	

		    $iconcontent = str_replace(array('&quot;','&#39;','\\'),array('"',"'",''),$iconcontent);			

		    $filename = $webroot . DS .$pagename;
		    $savefilename = $compeld;
		    $savedir = str_replace($pagename ,''  ,$savefilename);			
			

		    $tempfilename = '';				
		    if(is_file($savefilename) && @ filemtime($savefilename) > @ filemtime($filename))
		    {			
		        $tempfilename = $savefilename;				   			
		    } else	{
		        $tempfilename = $filename;					
		    }		
		    $fileContent = "";
		    if (!($fileContent = file_get_contents($tempfilename))) 
		    {
				return false;
		    }
				
		    $fileContent = preg_replace('/^(\xef\xbb\xbf)/', '', $fileContent); //EFBBBF
		    $fileContent = str_replace($strdata, $iconcontent, $fileContent);
		
		    if (!file_exists($savefilename)) {
                $CviewFileName = str_replace(array('\\','/'), DS ,$savefilename);
				$temp = explode(DS, $CviewFileName);
				array_pop( $temp );
                $tempFileDir = implode( DS.'' , $temp );
				get_dir($tempFileDir);
		    }
		
		    if (!file_put_contents($savefilename,  $fileContent)) {
			    return false;
		    }
		    echo '<script >alert("修改成功!");parent.location.reload();</script>';
		    exit;
/* 		     $notice = &get_message(array('msg'=>'修改成功','url'=>$this->_urldecode($fromurl),'title'=>'Notice'));
		    $notice ->display();	 */
   
	    }


		
 		$iconname = Request::$query['iconname'];
		
		$pagename = Request::$query['pagename'];	
			
		$strdata = Request::$query['data'];
		
		$weburl = Request::$query['weburl'];
		
		$webroot = Request::$query['webroot'];
		
		$act = Request::$query['act'];	
		
		$app = Request::$query['app'];
		
		$compeld = Request::$query['compeld'];		
		
		$compeld= $this->_urldecode($compeld);
		$compeld1 = $compeld;				
		$compeld= $this->_urlencode($compeld);


		$pagename= $this->_urldecode($pagename);
		$pagename1 = $pagename;				
		$pagename= $this->_urlencode($pagename);

		$webroot= $this->_urldecode($webroot);
		$webroot1 = $webroot;				
		$webroot= $this->_urlencode($webroot);		
	
		$weburl= $this->_urldecode($weburl);
		$fromurl = $weburl;				
		$weburl= $this->_urlencode($weburl);
		
		$divwidth = Request::$query['divwidth'];
				
		$divheight = Request::$query['divheight'];
		
		$strtempdata = $strdata;
		$urlstr="&weburl={$weburl}&compeld={$compeld}&app={$app}&act={$act}&webroot={$webroot}&iconname={$iconname}&pagename={$pagename}&data={$strdata}&divwidth={$divwidth}&divheight={$divheight}";

		$strdata = $this->filtrate($strdata);	

		$this->icon_to_array($strdata);
	    $data['config'] = $this->icon_to_array;
		if(!isset($data['config']['fields'])){
		    $data['config']['fields'] = '';
		}
		if(!isset($data['config']['limit'])){
		    $data['config']['limit'] = '';
		}		
        $iconfield =  filterEmptyVar(explode("|",$data['config']['fields']));
		$iconfielddata = array();
        foreach($iconfield as $key => $value)
		{
		   $temp = filterEmptyVar(explode(":",$value));
		   $iconfielddata[$temp[1]] = $temp[0];
		}
        $limitnum = filterEmptyVar(explode(",",$data['config']['limit']));

		if(empty($limitnum[1]))$limitnum[1]=10;
	    $limitnum = $limitnum[1];
	   
		$data['iconcontent'] = '<psp:tags '.$strdata.' />';
		$data['iconname'] = $iconname;	
			


		$code= file_get_contents($webroot1 . DS .'tags' . DS . $data['config']['htmlname'].'.html');	 
	
		$code = preg_replace('/^(\xef\xbb\xbf)/', '', $code); //EFBBBF

		
	
        $this->set('code', $code);
        $this->set('htmlname', $data['config']['htmlname']);		


		
	    $this->set('weburl', $weburl);
	    $this->set('fromurl', $fromurl);	
						
	    $this->set('urlstr', $urlstr);
		
	    
	    $this->set('compeld', $compeld);
	    $this->set('webroot', $webroot);
	    $this->set('app', $app);
	    $this->set('act', $act);
	
        $this->set('pagename', $pagename);			
        $this->set('strtempdata', $strtempdata);	
    }


    function viewmanageactiveedit()
	{
	
 		$iconname = Request::$query['iconname'];
		
		$pagename = Request::$query['pagename'];	
			
		$strdata = Request::$query['data'];
		
		$weburl = Request::$query['weburl'];
		
		$webroot = Request::$query['webroot'];
		
		$act = Request::$query['act'];	
		
		$app = Request::$query['app'];
		
		$compeld = Request::$query['compeld'];		
		
		$compeld= $this->_urldecode($compeld);
		$compeld1 = $compeld;				
		$compeld= $this->_urlencode($compeld);


		$pagename= $this->_urldecode($pagename);
		$pagename1 = $pagename;				
		$pagename= $this->_urlencode($pagename);

		$webroot= $this->_urldecode($webroot);
		$webroot1 = $webroot;				
		$webroot= $this->_urlencode($webroot);		
	
		$weburl= $this->_urldecode($weburl);
		$fromurl = $weburl;				
		$weburl= $this->_urlencode($weburl);
		
		$divwidth = Request::$query['divwidth'];
				
		$divheight = Request::$query['divheight'];
		
		$strtempdata = $strdata;
		$urlstr="&weburl={$weburl}&compeld={$compeld}&app={$app}&act={$act}&webroot={$webroot}&iconname={$iconname}&pagename={$pagename}&data={$strdata}&divwidth={$divwidth}&divheight={$divheight}";

	
		$strdata = $this->filtrate($strdata);	

		$this->icon_to_array($strdata);
		
	    $iconconfig = $this->icon_to_array;
        if(!isset($iconconfig['activetime'])) {
		    $iconconfig['activetime'] = ' , ';
		}
        $start_end_time =  filterEmptyVar(explode(",",$iconconfig['activetime']));
		$iconconfig['activetime_start'] = $start_end_time[0];
		$iconconfig['activetime_end'] = $start_end_time[1];

        $tempactivearray = array();
        $activearray = array();	
		
        $tempactivearray['after'] = $this->get_all_dirname($webroot1 . DS . 'active' . DS . 'after');
        $tempactivearray['before'] = $this->get_all_dirname($webroot1 . DS . 'active' . DS . 'before');
        $tempactivearray['period'] = $this->get_all_dirname($webroot1 . DS . 'active' . DS . 'period');
		foreach($tempactivearray as $key => $value)
		{
		     foreach($value as $key1 => $value1)
			 {
			   $activearray[$key][$key1] = str_replace(array('.active.'.$key.'.php'),array(''),$value1);
			 }		   	     
		}

	    $this->set('activearray', $activearray);
		
	    $this->set('data', $iconconfig);

	    $this->set('iconname', $iconname);
			
	    $this->set('weburl', $weburl);
	    $this->set('fromurl', $fromurl);	
							
	    $this->set('urlstr', $urlstr);
		
	    $this->set('compeld', $compeld);
	    $this->set('webroot', $webroot);
	    $this->set('app', $app);
	    $this->set('act', $act);
	
        $this->set('pagename', $pagename);			
        $this->set('strtempdata', $strtempdata);	
	    
	}	

    function viewmanageactiveeditpost()
    {

		    $pagename = $this->_urldecode(Request::$data['pagename']);
		 
		    $strtempdata = Request::$data['strtempdata'];
		 
		    $fromurl = $this->_urldecode(Request::$data['fromurl']); 
		 
		    $compeld = $this->_urldecode(Request::$data['compeld']); 

		    $webroot = $this->_urldecode(Request::$data['webroot']); 
		 


		    $app = Request::$data['app'];		 
		    $act = Request::$data['act'];			
	
		 		 	
		 $strdata = $this->filtrate($strtempdata);
		 $this->icon_to_array($strdata);
		
	     $iconconfig = $this->icon_to_array;		 	 
	     $data = Request::$data;
         unset($data['pagename'],$data['strtempdata'],$data['fromurl'],$data['compeld'],$data['webroot'],$data['app'],$data['act']);
		 	 
		 
		 if(!empty($data['activetime_start'])&&!empty($data['activetime_end']))
		 {
		    $data['activetime'] = $data['activetime_start'].','.$data['activetime_end'];
		 }else
		 {
		    $data['activetime'] = '';		 
		 }
		 unset($data['activetime_start'],$data['activetime_end']);

         $tempdata = $data + $iconconfig;
		 $tempdata = filterEmptyVar($tempdata); 
  

         $data['config'] = $tempdata;
         $data['iconcontent']='';
		 		 			 
		 foreach($data['config'] as $key => $value)
		 {
			 $data['iconcontent'] .= '  '.$key."=\'".$value."\'";
		 }			 	  	
	
		 $data['iconcontent'] = str_replace(array('&quot;','&#39;','\\'),array('"',"'",''),$data['iconcontent']);			


		    $filename = $webroot . DS .$pagename;
		    $savefilename = $compeld;
		    $savedir = str_replace($pagename ,''  ,$savefilename);			
		
		
		$tempfilename = '';				
		if(is_file($savefilename) && @ filemtime($savefilename) > @ filemtime($filename))
		{			
		     $tempfilename = $savefilename;				   			
		}
		else
		{
		     $tempfilename = $filename;					
		}
		
		$fileContent = "";
		if (!($fileContent = file_get_contents($tempfilename))) 
		{
				return false;
		}
				
		$fileContent = preg_replace('/^(\xef\xbb\xbf)/', '', $fileContent); //EFBBBF
		$fileContent = str_replace($strdata, $data['iconcontent'], $fileContent);
		
		if (!file_exists($savefilename)) {
                $CviewFileName = str_replace(array('\\','/'), DS ,$savefilename);
				$temp = explode(DS, $CviewFileName);
				array_pop( $temp );
                $tempFileDir = implode( DS.'' , $temp );
				get_dir($tempFileDir);
		}
		
		if (!file_put_contents($savefilename, $fileContent)) {
			return false;
		}
		echo '<script >alert("修改成功!");parent.location.reload();</script>';
		exit;
/* 		$notice = &get_message(array('msg'=>'修改成功','url'=>$this->_urldecode($fromurl),'title'=>'Notice'));
		$notice ->display();	 */


    }



    function viewmanagetableedit()
    {
        $changeClassName = @include(CONFIGS . DS . 'tableTag.php');	
/* 	    $changeClassName = array(
	        array('name' =>'产品管理',
		       'table' =>'goods',
			   'filed1' =>'goods.id',
			   'filed2' =>'goods.goods_name',
			   'conditions' =>'',
			   'cattable' =>'category',
			   'catfiled1' =>'id',
			   'catfiled2' =>'cat_name',
			   'catparent' =>'parent_id',			   
			   'catconditions' =>'',
			   'lconditions' =>'goods.cat_id',		   
	        ),
	        array('name' =>'店铺管理',
		       'table' =>'shop_info',
			   'filed1' =>'shop_info.id',
			   'filed2' =>'shop_info.shop_name',			   
	        ),	
	        array('name' =>'文章管理',
		       'table' =>'article',
			   'filed1' =>'article.id',
			   'filed2' =>'article.title',			   
	        ),
	        array('name' =>'店铺文章管理',
		       'table' =>'shop_article.cat_id|shop_article_cat.id',
			   'filed1' =>'shop_article.id',
			   'filed2' =>'shop_article.title',			   
	        ),			
	        array('name' =>'|--回复',
		       'table' =>'post.id|hui.post_id',
			   'filed1' =>'post.id',
			   'filed2' =>'hui.content',			   
	        ),				
	        array('name' =>'|--消费记录表',
		       'table' =>'users.user_id|usermoney.userid',
			   'filed1' =>'usermoney.mid',
			   'filed2' =>'usermoney.intro',			   
	        ),		  
	    ); */
	
        if(!isset(Request::$query['dopost']))Request::$query['dopost']='';
	    $action = Request::$query['dopost'];
 	    if($action=='post')
	    {
		    $pagename = $this->_urldecode($_POST['pagename']);		 
		    $strtempdata = $_POST['strtempdata'];		 
		    $fromurl = $this->_urldecode($_POST['fromurl']); 
		    $compeld = $this->_urldecode($_POST['compeld']); 
		    $webroot = $this->_urldecode($_POST['webroot']); 
		    $app = $_POST['app'];		 
		    $act = $_POST['act'];
			
	        $fids = Request::get_args('fids');
	        $cids = Request::get_args('cids');			
            $dorder = Request::get_args('dorder');
            $dconditions = Request::get_args('dconditions');
            $userorder = '';
            $userconditions = '';			
            if(!empty($dorder)){
		        foreach($dorder as $key => $value) {
				    if($value!='0') {
		                $userorder .= $key.' '.$value.','; 					
					}
		        }
                $userorder = substr($userorder,0,-1);					
			}
            if(!empty($dconditions)){
		        foreach($dconditions as $key => $value) {
				    if($value!='0') {
		                $userconditions .= $key.' and '; 					
					}
		        }
                $userconditions = substr($userconditions,0,-4);					
			}			
	        $tplpath = $_POST['tplpath'];		 
	        $tplpart_1 = $_POST['tplpart_1'];		 
	        $data = $_POST['data'];
            unset($data['dopost']);			
	        $fenorxin = $_POST['fenorxin'];
	        $mokuai = $_POST['mokuai'];
	        $limitnum = $_POST['limitnum'];
			
		    if(empty($limitnum)) {
		        $limitnum = 10;
		    }
		 		 
		    $strdata = $this->filtrate($strtempdata);	 
            $this->icon_to_array($strdata);
	        $old_icondata = $this->icon_to_array;

		    $fields_str = '';
		    if(isset($data)&&!empty($data)) {
		        foreach($data as $key => $value) {
				    if($value['datavalue'] && $value['datakey']) {
		            $fields_str .= $value['datavalue'].':'.$value['datakey'].'|'; 
					}
		        }
		    }
		    $old_icondata['htmlname'] = str_replace('.html','',basename($tplpath));
		    $old_icondata['fields'] = $fields_str;
		    $old_icondata['limit']=$limitnum;			 	
            if($mokuai==1) {
  		        $temp_html_name = filterEmptyVar(explode(".",$old_icondata['htmlname']));
		        $old_icondata['htmlname'] = $temp_html_name[0];
				
				$icon_dir = $webroot . DS . 'tags';
		        $iconfile = $old_icondata['htmlname'].'.'.time().'.html';
		        $iconfilename = $old_icondata['htmlname'].'.'.time().'';
		        $old_icondata['htmlname'] = $iconfilename;			
	            $temparray = $this->get_all_dirname($icon_dir);
                $is = 1;
	            foreach($temparray as $key => $value) {
		            if($value==$iconfile)
		            {
                        $is = 0;break;
		            }	
                }
			
		        if($is)
		        {
                    $iconhtml = stripslashes($tplpart_1);
		            $iconhtml = str_replace(array('&quot;','&#39;'),array('"',"'"),$iconhtml);				
                    $iconhtmlarray = @include($icon_dir . DS .'tags.php');
		            $iconhtmlarray[$iconfilename]['name'] =$iconfilename;
                    $iconhtmlarray[$iconfilename]['html'] =$iconhtml;
				    $iconhtmlarray[$iconfilename]['config'] =array();

					if (!(file_put_contents($icon_dir . DS .'tags.php', "<?php \nreturn " . var_export($iconhtmlarray , true) . ";\n?>")
					     && file_put_contents($icon_dir.DS.$iconfile,$iconhtml))
						) {
			            return false;
		            }					
	
		        } else {
		            echo '<script >alert("Icondata名称有相同，请重新提交!");parent.location.reload();</script>';
		            exit;					
		        }
		     
		    }else if ($mokuai==0) {
		        $icon_dir = $webroot . DS . 'tags';
		        $iconfile = $old_icondata['htmlname'].'.html';
		        $iconfilename = $old_icondata['htmlname'];		 
                $iconhtml = stripslashes($tplpart_1);
		        $iconhtml = str_replace(array('&quot;','&#39;'),array('"',"'"),$iconhtml);				
                $iconhtmlarray = @include($icon_dir . DS .'tags.php');
		        $iconhtmlarray[$iconfilename]['name'] =$iconfilename;
                $iconhtmlarray[$iconfilename]['html'] =$iconhtml;
				$iconhtmlarray[$iconfilename]['config'] =array();
				if (!(file_put_contents($icon_dir . DS .'tags.php', "<?php \nreturn " . var_export($iconhtmlarray , true) . ";\n?>")
					     && file_put_contents($icon_dir.DS.$iconfile,$iconhtml))
					) {
			        return false;
		        }			 
		    }
			
			if($fenorxin=='1') {
				$temp_table_filed_id = array();
				if(isset($cids[0])) {
				    $conditions = $userconditions;	
					$temp_fids_array = filterEmptyVar(explode(",",$cids[0]));
					if(!isset($temp_fids_array[0]))$temp_fids_array[0]='';
					if(!isset($temp_fids_array[1]))$temp_fids_array[1]='';			   
					$temp_table_filed_id['tablename'] = $temp_fids_array[0];
					$temp_table_filed_id['filedname'] = $temp_fids_array[1];
					$temp_table_filed_id['id'] = '';
					foreach($cids as $key=>$value) {
						$temp_fids_array = filterEmptyVar(explode(",",$value));			   
						if(!isset($temp_fids_array[2]))$temp_fids_array[2]='';
                        if(!empty($temp_fids_array[2]))	$temp_table_filed_id['id'] .= $temp_fids_array[2].',';							   			   			    
					}					
					if(!empty($temp_table_filed_id['id'])) {
					    $temp_table_filed_id['id'] = substr($temp_table_filed_id['id'],0,-1);
					}
					
					$old_icondata['tablename'] = $temp_table_filed_id['tablename'];	
					$changevalue = array();			
					if(!empty($temp_table_filed_id['tablename'])&&!empty($temp_table_filed_id['filedname'])){     
						foreach($changeClassName as $value) {
							if($value['table']==$temp_table_filed_id['tablename'] && ($value['filed1'].'|'.$value['filed2'])==$temp_table_filed_id['filedname'] ) {
								$changevalue = $value;
							}
						}				
						if(isset($temp_table_filed_id['id'])&&!empty($temp_table_filed_id['id']) && !empty($changevalue)){
						    if(empty($conditions))$conditions .=' 1 ';
							$conditions .= ' and '.$changevalue['lconditions'].' in('.$temp_table_filed_id['id'].') ' ;
						}					
					}					
			        $old_icondata['conditions']= $conditions;
			        $old_icondata['order']= $userorder;						
			        $old_icondata['limit']= $limitnum;	
			        $old_icondata['pageurl']= '';
                }					
			}else {
				$temp_table_filed_id = array();
				if(isset($fids[0])) {			
					$temp_fids_array = filterEmptyVar(explode(",",$fids[0]));
					if(!isset($temp_fids_array[0]))$temp_fids_array[0]='';
					if(!isset($temp_fids_array[1]))$temp_fids_array[1]='';			   
					$temp_table_filed_id['tablename'] = $temp_fids_array[0];
					$temp_table_filed_id['filedname'] = $temp_fids_array[1];
					$temp_table_filed_id['id'] = '';
					foreach($fids as $key=>$value) {
						$temp_fids_array = filterEmptyVar(explode(",",$value));			   
						if(!isset($temp_fids_array[2]))$temp_fids_array[2]='';	   	
                        if(!empty($temp_fids_array[2]))	$temp_table_filed_id['id'] .= $temp_fids_array[2].',';							
					}
					if(!empty($temp_table_filed_id['id'])) {
					    $temp_table_filed_id['id'] = substr($temp_table_filed_id['id'],0,-1);
					}
					if(!empty($temp_table_filed_id['id'])) {
					    $conditions = ' '.$temp_table_filed_id['filedname'].' in('.$temp_table_filed_id['id'].')';
					}					
					$old_icondata['tablename'] = $temp_table_filed_id['tablename'];							
			        $old_icondata['conditions']=$conditions;
			        $old_icondata['order']='';				
			        $old_icondata['limit']='';			
			    }
			}

            $iconcontent = '';
		    foreach($old_icondata as $key => $value) {
		        $iconcontent .= '  '.$key."=\'".$value."\'";
		    }			 	  	
      
		    $iconcontent = str_replace(array('&quot;','&#39;','\\'),array('"',"'",''),$iconcontent);			


		    $filename = $webroot . DS .$pagename;
		    $savefilename = $compeld;
		    $savedir = str_replace($pagename ,''  ,$savefilename);				
			
		    $tempfilename = '';				
		    if(is_file($savefilename) && @ filemtime($savefilename) > @ filemtime($filename))
		    {			
		        $tempfilename = $savefilename;				   			
		    } else {
		        $tempfilename = $filename;					
		    }		
		    $fileContent = "";
		    if (!($fileContent = file_get_contents($tempfilename))) {
				return false;
		    }
				
		    $fileContent = preg_replace('/^(\xef\xbb\xbf)/', '', $fileContent); //EFBBBF
		    $fileContent = str_replace($strdata, $iconcontent, $fileContent);
		
		    if (!file_exists($savefilename)) {
                $CviewFileName = str_replace(array('\\','/'), DS ,$savefilename);
				$temp = explode(DS, $CviewFileName);
				array_pop( $temp );
                $tempFileDir = implode( DS.'' , $temp );
				get_dir($tempFileDir);
		    }
		
		    if (!file_put_contents($savefilename,  $fileContent)) {
			    return false;
		    }
		    echo '<script >alert("修改成功!");parent.location.reload();</script>';
		    exit;
/* 		    $notice = &get_message(array('msg'=>'修改成功','url'=>$this->_urldecode($fromurl),'title'=>'Notice'));
		    $notice ->display();	 */
   
	    } 
		
        if($action=="getTplcode")
	    {
	        $divid = Request::$query['divid'];	
	        $filepath = Request::$query['filepath'];

   		   $filepath = str_replace($this->base,'',$filepath);

           $filepath =str_replace(DS,'/', APP_ROOT.$filepath);	 	
	       $code =  file_get_contents($filepath);
		    $code= str_replace(array("\r","\n","'"),array('\r','\n',"\'"),$code );

			
    	    echo"<SCRIPT LANGUAGE=\"JavaScript\">
	            <!--
	            parent.puthtmlcode('$code');
	            //-->
	            </SCRIPT>";
	        exit;
        }	   
	   			 	   
        if($action=='getfidsonslist')
	    { 
	        $fup = Request::$query['fup'];
		    $class = Request::$query['class'];
		    $ctype = Request::$query['ctype'];
		    $page =  Request::$query['page'];
		    $keyword =  Request::$query['keyword'];			
		    $Murl = $this->base.'/index.php/tagsedit/viewmanagetableedit/';

	        $tablearray = array();

		    $DbConfig = 'default';
    		
	        $db =& ConnectionManager::getDataSource($DbConfig);
		    $DbPrefix = $db->config['prefix'];		
		
	        $temparray = $db->listSources();
		    foreach($temparray as $key => $value)
		    {
			    if($value)
			    {
		            $temp_str = str_replace($DbPrefix,'',$value);
				    $temp_array = $db->TableDescribe($value);
 		            $tablearray[$value][$value] = $temp_str; 

		            foreach($temp_array as $key1=>$value1)
				    {
			            $tablearray[$value][$value.'.'.$value1] = $temp_str.'.'.$value1;

			        }
			    }

		    }			
			  
            header('Content-type: text/html; charset='.Param::read('App.encoding'));	
		  
  		    $tables_fileds = filterEmptyVar(explode(",",$fup));

		    $tablename_array = array();
		    $fields_array = array();
		    $temp_fields_array = array();
		    $temp_fields_array1 = array();		 		 
            $data = array();
		 	 
            if(isset($tables_fileds[0])&&!empty($tables_fileds[0]))
		    {
		        $temp = $temp1 = array();	
		        $temp = explode("|",$tables_fileds[0]);			
                $temp = filterEmptyVar($temp);
			    $num=1;
                foreach($temp as $key => $value)
			    {
		            $temp1 = explode(".",$value);
				    if(isset($temp1[1]))
				    {
				        $tablename_array[$num]['table'] = $temp1[0];
				        $tablename_array[$num]['field'] = $temp1[1];
				    } else {
				        $tablename_array[$num]['table'] = $temp1[0];
				        $tablename_array[$num]['field'] = 0;			
				    }
				    $num++;
			    }

		    }
	
            if(isset($tables_fileds[1])&&!empty($tables_fileds[1]))
		    {
		        $temp = $temp1 = array();	
		        $temp = explode("|",$tables_fileds[1]);			
                $temp = filterEmptyVar($temp);
			
                foreach($temp as $key => $value)
			    {
		            $temp1 = explode(":",$value);
				    if(isset($temp1[1]))
				    {
				        $fields_array[$temp1[0]] = $temp1[1];
				    }else{
				        $fields_array[$temp1[0]] = 0;				
				    }
				    $temp3 = explode(".",$temp1[0]);
                    $temp_fields_array1[$key] = $temp1[0];								
                    $temp_fields_array[$key] = $temp3[1];				
			    }
		    }


			$changevalue = array();			
		    if(isset($tables_fileds[0])&&isset($tables_fileds[1])&&!empty($tables_fileds[0])&&!empty($tables_fileds[1])){     
			    $temp_on = '';
			    $table_pre_key = '';
			    $table_pre_value = '';
				$conditions =' 1 ';

				foreach($changeClassName as $value) {
					if($value['table']==$tables_fileds[0] && ($value['filed1'].'|'.$value['filed2'])==$tables_fileds[1] ) {
						$changevalue = $value;
				    }
				}				
                if(isset($tables_fileds[2])&&!empty($tables_fileds[2])){
					if(!empty($changevalue)) {
					    $conditions .= ' and '.$changevalue['lconditions'].'='.$tables_fileds[2].' ' ;
					}
				}				
                if(isset($changevalue['conditions']) && !empty($changevalue['conditions'])) {
                    $conditions .= ' and '.$changevalue['conditions'];
                }	
				if(!empty($keyword) && isset($temp_fields_array1[1])) {
                    $conditions .= ' and '.$temp_fields_array1[1].' like"%'.$keyword.'%"';				     
				}
                if(count($tablename_array)==1)
			    {
			        $tables = $DbPrefix.$tablename_array[1]['table']. ' '.$tablename_array[1]['table'];
			    } else {
                    for($num=1;$num<=count($tablename_array);$num++)
		            {
				        if(empty($tablename_array[$num]['field'])){$tablename_array[$num]['field'] = ' ';}

                        if(isset($tablename_array[$num+1])&&$num==1) {	

                            $tables.= $DbPrefix.$tablename_array[$num]['table'] . ' '.$tablename_array[$num]['table'].' LEFT JOIN '.$DbPrefix.$tablename_array[$num+1]['table'] . ' '.$tablename_array[$num+1]['table'].' ON '. $tablename_array[$num]['table'] .'.'.$tablename_array[$num]['field'].'='. $tablename_array[$num+1]['table'] .'.'.$tablename_array[$num+1]['field'].' ';
				  
				        } else {   
					        if(isset($tablename_array[$num+1]))
					        {
	                          $tables.= ' LEFT JOIN '.$DbPrefix.$tablename_array[$num+1]['table'] . ' '.$tablename_array[$num+1]['table'].' ON '. $tablename_array[$num]['table'] .'.'.$tablename_array[$num]['field'].'='. $tablename_array[$num+1]['table'] .'.'.$tablename_array[$num+1]['field'].' ';						  
					        }
		  
				        }
		            }	
			    }

                foreach($fields_array as $key => $value)
		        {
			        if(empty($value))
			        {
                        $fields.= $key . ' ,';			
			        }else {
                        $fields.= $key . ' as '.$value.' ,';			
			        }
		        }
  			    $fields = substr($fields, 0,-1);
                $data = $this->TagsModel(array("table" => $tables,"fields" => $fields,"conditions" => $conditions,"order" => '',"limit" => '20',"page" => $page));  
		    }

            $user_order = "";
            $user_conditions = "";			
		    if(!empty($changevalue)) {
			    if(isset($changevalue['userorder']) && !empty($changevalue['userorder']) ) {				   
					foreach($changevalue['userorder'] as $key1 => $value1)
					{			             
						$user_order .= $value1.':<select name="dorder['.$key1.']"><option  value="0">不排序</option><option  value="asc">从高到低</option><option  value="desc">从低到高</option></select>&nbsp;&nbsp;';	   
					}				   
				}	
			    if(isset($changevalue['userconditions']) && !empty($changevalue['userconditions']) ) {				   
					foreach($changevalue['userconditions'] as $key1 => $value1)
					{			             
						$user_conditions .= '<input type="checkbox" name="dconditions['.$key1.']" value="1" >'.$value1.'&nbsp;&nbsp;';	   
					}				   
				}				
			}

		    $select_str = "";
		    foreach($tablename_array as $key => $value)
		    {		    
		        foreach($tablearray[$DbPrefix.$value['table']] as $key1 => $value1)
		        {			             
                    $select_str .= '<option  value="'.$value1.'">&nbsp;|&nbsp;&nbsp;&nbsp;'.$key1.'</option>';	   
		        }
			    $select_str .= '<optgroup label="===================" style="color:red;"></optgroup>';	 

		    }

            $str='';			
			$str.= "<div style=\'color:red;\'>关键字:<input type=\'text\' class=\'txt\' name=\'keyword\' value=\'".$keyword."\' style=\'width:150px;height=30px;\' onchange=\"changekeyword(1,this,\'".$fup."\',true,0 ,\'$Murl\',\'1\')\" ></div>"; 			
		    foreach($data['data'] as $key => $value)
		    {
		        $next='false';
		        $icon_fup = true;
				if($value[$temp_fields_array[0]])
				{
				$str.= "<div><a href=\"javascript:;\" onclick=\"changeClassName($class,this,\'".$tables_fileds[0].','.$temp_fields_array1[0].','.$value[$temp_fields_array[0]]."\',$next,$ctype ,\'$Murl\')\">".'['.$value[$temp_fields_array[0]].']'.$value[$temp_fields_array[1]]."</a></div>"; 				
				}
		    }
			if(!empty($data['data'])) {			
			$str.= "<div><a style=\'color:red;\' href=\"javascript:;\" onclick=\"changeClassName(1,this,\'".$fup."\',true,0 ,\'$Murl\',\'".($data['page']['current']['num']-1)."\',\'$keyword\')\">上一页[第".$data['page']['current']['num']."页,共".$data['page']['pageCount']."页]</a></div>"; 	
			$str.= "<div><a style=\'color:red;\' href=\"javascript:;\" onclick=\"changeClassName(1,this,\'".$fup."\',true,0 ,\'$Murl\',\'".($data['page']['current']['num']+1)."\',\'$keyword\')\">下一页[共".$data['page']['total']."条]</a></div>"; 			
	        }else {
			$str.= "<div><a style=\'color:red;\' href=\"javascript:;\" onclick=\"changeClassName(1,this,\'".$fup."\',true,0 ,\'$Murl\',\'1\',\'$keyword\')\">首页[第".$data['page']['current']['num']."页,共".$data['page']['pageCount']."页,共".$data['page']['total']."条]</a></div>"; 					
			}
			$str=$str?$str:"&nbsp;";
	        echo '<script >parent.inputcontent(\''.$str.'\','.$class.',\''.$select_str.'\',\''.$user_order.'\',\''.$user_conditions.'\');</script>';
	        exit;	
        }
  
 		$iconname = Request::$query['iconname'];
		
		$pagename = Request::$query['pagename'];	
			
		$strdata = Request::$query['data'];
		
		$weburl = Request::$query['weburl'];
		
		$webroot = Request::$query['webroot'];
		
		$act = Request::$query['act'];	
		
		$app = Request::$query['app'];
		
		$compeld = Request::$query['compeld'];		
		
		$compeld= $this->_urldecode($compeld);
		$compeld1 = $compeld;				
		$compeld= $this->_urlencode($compeld);


		$pagename= $this->_urldecode($pagename);
		$pagename1 = $pagename;				
		$pagename= $this->_urlencode($pagename);

		$webroot= $this->_urldecode($webroot);
		$webroot1 = $webroot;				
		$webroot= $this->_urlencode($webroot);		
	
		$weburl= $this->_urldecode($weburl);
		$fromurl = $weburl;				
		$weburl= $this->_urlencode($weburl);
		
		$divwidth = Request::$query['divwidth'];
				
		$divheight = Request::$query['divheight'];
	
		$strtempdata = $strdata;
		$urlstr="&weburl={$weburl}&compeld={$compeld}&app={$app}&act={$act}&webroot={$webroot}&iconname={$iconname}&pagename={$pagename}&data={$strdata}&divwidth={$divwidth}&divheight={$divheight}";


  		
		$strdata = $this->filtrate($strdata);	

		$this->icon_to_array($strdata);
	    $data['config'] = $this->icon_to_array;
		if(!isset($data['config']['fields'])){
		   $data['config']['fields'] = '';
		}
		if(!isset($data['config']['limit'])){
		    $data['config']['limit'] = '';
		}			
        $iconfield =  filterEmptyVar(explode("|",$data['config']['fields']));
		$iconfielddata = array();
        foreach($iconfield as $key => $value)
		{
		   $temp = filterEmptyVar(explode(":",$value));
		   $iconfielddata[$temp[1]] = $temp[0];
		}
		$limitnum = intval(trim($data['config']['limit'])) ;
/* 		if(is_numeric($data['config']['limit'])) {
		    $limitnum = intval($data['config']['limit']) ;
		}else {
            $limitnum = filterEmptyVar(explode(",",$data['config']['limit']));
		    if(empty($limitnum[1]))$limitnum[1]=10;
	        $limitnum = $limitnum[1];		
		} */

	   
		$data['iconcontent'] = '<psp:tags '.$strdata.' />';
		$data['iconname'] = $iconname;	

	    $tablearray = array();

		$DbConfig = 'default';
    		
	    $db =& ConnectionManager::getDataSource($DbConfig);
		$DbPrefix = $db->config['prefix'];		
		
	    $temparray = $db->listSources();
		foreach($temparray as $key => $value)
		{
			if($value)
			{
		        $temp_str = str_replace($DbPrefix,'',$value);
				$temp_array = $db->TableDescribe($value);
 		        $tablearray[$value][$value] = $temp_str; 

		        foreach($temp_array as $key1=>$value1)
				{
			     $tablearray[$value][$value.'.'.$value1] = $temp_str.'.'.$value1;

			    }
			}

		}		
        $changeClassName_data = array();
        foreach($changeClassName as $key => $value ){
  		    if(isset($value['cattable']) && isset($value['catfiled1']) && isset($value['catfiled2']) && isset($value['lconditions'])&& isset($value['catparent']) 
			   && !empty($value['cattable']) && !empty($value['catfiled1']) && !empty($value['catfiled2']) && !empty($value['lconditions']) && !empty($value['catparent'])){
			   if(!isset($value['catconditions']))$value['catconditions']='';
		        $chag_query = "SELECT * FROM ".$DbPrefix.$value['cattable']." ".$value['catconditions'];
		        $result_chag = $db->QueryAll($chag_query);	
                foreach($result_chag as $v) {
	                $changeClassName_data[$key][$v[$value['catparent']]][$v['id']] = $v;
                }
				
			}
	    }		

        $cachearray = array();

        $htmlnamearray = array();
        $temparray = array();
        $temparray1 = array();
		
        $temparray = $this->get_all_dirname($webroot1 . DS .'tags');
        $temparray1 = include($webroot1 . DS .'tags' . DS . 'tags.php');
		foreach($temparray1 as $key => $value)
		{

		   $htmlnamearray[$key] = $key.'.html';
		}

	    foreach($temparray as $key => $value)
		{
		   if(strpos($value,'.html'))
		   {
		    $htmlnamearray[str_replace('.html','',$value)] = $value;
		   }
		}

        $icondataarray = array();
        $temparray = array();
        $temparray = $this->get_all_dirname($webroot1 . DS .'tags' . DS . 'tempdata');
	    foreach($temparray as $key => $value)
		{
		   if(strpos($value,'.tags')) {
		    $icondataarray[str_replace('.tags','',$value)] = $value;
		   }
		}

		$code= file_get_contents($webroot1 . DS .'tags' . DS . $data['config']['htmlname'].'.html');	

        $icontabledata = '';
		$tempclass_name = array();		
        if(isset($data['config']['tablename'])&&!empty($data['config']['tablename']))
		{	
		
		    $sql_order ='';
		    $sql_limit = '';
		    $sql_conditions = '';
		    $sql_table = '';
		    $sql_field = '';
			$tableid = '';
			$sql = '';
			
					
		    $tablename_array = array();
		    $fields_array = array();
		    $fields_temp_array = array();								
		    $temp = $temp1 = array();	
		    $temp = explode("|",$data['config']['tablename']);			
            $temp = filterEmptyVar($temp);
			$num=1;
            foreach($temp as $key => $value)
			{
		        $temp1 = explode(".",$value);
				if(isset($temp1[1]))
				{
				$tablename_array[$num]['table'] = $temp1[0];
				$tablename_array[$num]['field'] = $temp1[1];
				}
				else
				{
				$tablename_array[$num]['table'] = $temp1[0];
				$tablename_array[$num]['field'] = 0;			
				}
				$num++;
			}
            $isset_changeClassName = false;
            foreach($changeClassName as $key => $value )
			{
  		       if($value['table']==$data['config']['tablename'])
			   {
			     $categorydata =  $value['name'];
				 $tempclass_name = $value;
			     $fields_array[$value['filed1']] = 0;
			     $fields_array[$value['filed2']] = 0;
				 $temp = explode(".",$value['filed1']);
				 $fields_temp_array[1]= $temp[1];
				 $temp = explode(".",$value['filed2']);
				 $fields_temp_array[2]= $temp[1];
                $isset_changeClassName = true;				 
			   }
			}
			
            if(count($tablename_array)==1)
			{
			   $sql_table = $DbPrefix.$tablename_array[1]['table']. ' '.$tablename_array[1]['table'];
			}
			 else
			{
                for($num=1;$num<=count($tablename_array);$num++)
		        {
				  if(empty($tablename_array[$num]['field'])){$tablename_array[$num]['field'] = ' ';}

                  if(isset($tablename_array[$num+1])&&$num==1)
				  {	

                  $sql_table.= $DbPrefix.$tablename_array[$num]['table'] . ' '.$tablename_array[$num]['table'].' LEFT JOIN '.$DbPrefix.$tablename_array[$num+1]['table'] . ' '.$tablename_array[$num+1]['table'].' ON '. $tablename_array[$num]['table'] .'.'.$tablename_array[$num]['field'].'='. $tablename_array[$num+1]['table'] .'.'.$tablename_array[$num+1]['field'].' ';
				  
				  }
				  else
				  {   
					  if(isset($tablename_array[$num+1]))
					  {
	                  $sql_table.= ' LEFT JOIN '.$DbPrefix.$tablename_array[$num+1]['table'] . ' '.$tablename_array[$num+1]['table'].' ON '. $tablename_array[$num]['table'] .'.'.$tablename_array[$num]['field'].'='. $tablename_array[$num+1]['table'] .'.'.$tablename_array[$num+1]['field'].' ';						  
					  }
		  
				  }


		        }	
			  
			}
            foreach($fields_array as $key => $value)
		    {
			   if(empty($value))
			   {
                $sql_field.= $key . ' ,';			
			   }else
			   {
                $sql_field.= $key . ' as '.$value.' ,';			
			   }
		    }
  			$sql_field = substr($sql_field, 0,-1);
		
			
            if(isset($data['config']['order']))
		    {
		      $sql_order = ''.$data['config']['order'];		
		    }
            if(isset($data['config']['limit']))
		    {
		      $sql_limit = ''.$data['config']['limit'];		
		    }
            if(isset($data['config']['conditions']))
		    {
		      $sql_conditions = ''.$data['config']['conditions'];		
		    }

		
           ($sql_conditions=='')?$sql_conditions = '':$sql_conditions = '  WHERE  '.$sql_conditions;	
            $sql_order && $sql_order = ' ORDER BY ' . $sql_order . ' ';
            $sql_limit && $sql_limit = " LIMIT 0,{$sql_limit} ";

			$tempdata =array();
		    if($isset_changeClassName){
		        $query = "SELECT {$sql_field} FROM {$sql_table} {$sql_conditions} {$sql_order} {$sql_limit}";			
			    $tempdata = $db->QueryAll($query);
            }				
			
			foreach($tempdata as $key => $value)
			{
			    if(isset($value[$fields_temp_array[1]])&&!empty($value[$fields_temp_array[1]]))
				{
			    $icontabledata .= '<div id="div_'.$tempclass_name['table'].','.$tempclass_name['filed1'].','.$value[$fields_temp_array[1]].'"><input type="checkbox" name="fids[]" value="'.$tempclass_name['table'].','.$tempclass_name['filed1'].','.$value[$fields_temp_array[1]].'" checked="" onclick="removeFid(this);">  <font color="#232323"> &gt; '.$categorydata.' &gt; ['.$value[$fields_temp_array[1]].']'.$value[$fields_temp_array[2]].'</font></div>';				
				}else
				{
				$icontabledata .= '';
				}
			
			}
       
		}
		$is_fenorxin = 1;
		if(!empty($tempclass_name) && isset($data['config']['conditions'])&&!empty($data['config']['conditions'])&& strstr($data['config']['conditions'],$tempclass_name['filed1']))
		{
			$is_fenorxin = 0;
		}
		$conitions_catids = array();//得到条件属性中的分类id号
		if(!empty($tempclass_name) && isset($data['config']['conditions'])&&!empty($data['config']['conditions'])&& strstr($data['config']['conditions'],$tempclass_name['lconditions'])) {
		    $temp_catids = explode($tempclass_name['lconditions'],$data['config']['conditions']);
			foreach($temp_catids as $v) {
				$v = trim($v);
				if(strpos($v,'=')===0){
					$v = trim(str_replace('=','',$v));
					$t = explode(' ',$v);
					if(isset($t[0])&& !empty($t[0])) {
						$t = trim($t[0]);
						$conitions_catids[$t] = $t;			
					}
				}else if(strpos($v,'in')===0) {
					$v = trim(str_replace('in','',$v));		
					if(strpos($v,'(')===0) {
						$v = trim(str_replace('(','',$v));
						$t = explode(')',$v);
						if(isset($t[0])&& !empty($t[0])) {
							$t = trim($t[0]);
							if(!empty($t)) {
								$t = explode(',',$t);
								foreach($t as $v1) {
									$conitions_catids[$v1] = $v1;	
								}
							}
									
						}			
					}
				}
			}
			if(!empty($conitions_catids)) {
			    foreach($conitions_catids as $v) {
				    $conitions_catids[$tempclass_name['table'].','.$tempclass_name['filed1'].'|'.$tempclass_name['filed2'].','.$v] = $tempclass_name['table'].','.$tempclass_name['filed1'].'|'.$tempclass_name['filed2'].','.$v;
				}
			}
		}
        $this->set('conitions_catids', $conitions_catids);	
		
		$user_order = "";
		$user_conditions = "";
		if(!empty($tempclass_name)) {
			if(isset($tempclass_name['userorder']) && !empty($tempclass_name['userorder']) ) {
                if(!isset($data['config']['order']))$data['config']['order'] = '';			
				$ot = explode(",",$data['config']['order']);				
				foreach($tempclass_name['userorder'] as $key => $value)
				{	
					$otis = '0';
					foreach($ot as $v) {
						if(strstr($v,$key)) {
							$otis = trim(str_replace($key,'',$v));
						}
					}						
					$user_order .= $value.':<select name="dorder['.$key.']">';
					$user_order .= '<option  value="0" '.(($otis=='0')?'selected':'').'>不排序</option>';
					$user_order .= '<option  value="asc" '.(($otis=='asc')?'selected':'').'>从高到低</option>';
					$user_order .= '<option  value="desc" '.(($otis=='desc')?'selected':'').'>从低到高</option></select>&nbsp;&nbsp;';	   
				}				   
			}			    

			if(isset($tempclass_name['userconditions']) && !empty($tempclass_name['userconditions']) ) {
			    if(!isset($data['config']['conditions']))$data['config']['conditions'] = '';	
				$ot = $data['config']['conditions'];					
				foreach($tempclass_name['userconditions'] as $key => $value)
				{	
					$otis = '';
					if(strstr($ot,$key)) {
						$otis = ' checked ';
					}
					$user_conditions .= '<input type="checkbox" name="dconditions['.$key.']" value="1" '.$otis.' >'.$value.'&nbsp;&nbsp;';	   
				}				   
			}				

		}
        $this->set('user_order', $user_order);		
        $this->set('user_conditions', $user_conditions);
		if(isset($tempclass_name['table'])) {
        $this->set('nowclass_name', $tempclass_name['table'].','.$tempclass_name['filed1'].'|'.$tempclass_name['filed2']);		
		}else {
        $this->set('nowclass_name', '');		
		}
		$relativepath = str_replace(APP_ROOT,'',$webroot1 . DS .'tags');
		
		$relativepath = str_replace(DS,'/',$relativepath);
		
        $yuanrelativepath = $relativepath .'/';		
        $bianrelativepath = str_replace('/','\/',$yuanrelativepath);		
			
        $this->set('yuanrelativepath', $yuanrelativepath);		
        $this->set('bianrelativepath', $bianrelativepath);
		
        $this->set('relativepath', $relativepath);
		
        $this->set('is_fenorxin', $is_fenorxin);		
		
        $this->set('is_fenorxin', $is_fenorxin);		
        $this->set('icontabledata', $icontabledata);
	
        $this->set('code', $code);
        $this->set('htmlname', $data['config']['htmlname']);
				
        $this->set('changeClassName', $changeClassName);
        $this->set('changeClassName_data', $changeClassName_data);		
		
        $this->set('DbPrefix', $DbPrefix);		
		
        $this->set('icondataarray', $icondataarray);
        $this->set('htmlnamearray', $htmlnamearray);
        $this->set('cachearray', $cachearray);
		if(!isset($tablename_array)){
		    $tablename_array = array();
		}
		if(!isset($data['config']['tablename'])){
		    $data['config']['tablename'] = '';
		}	
        $this->set('tablearray', $tablearray);
        $this->set('tablename_array', $tablename_array);
				
        $this->set('tablename', $DbPrefix.$data['config']['tablename']);
		     
        $this->set('viewdata', $this->get_all_dirname($webroot1));

	    $this->set('iconfielddata', $iconfielddata);

	    $this->set('limitnum', $limitnum);
		
	    $this->set('data', $data);
	    $this->set('weburl', $weburl);
	    $this->set('fromurl', $fromurl);	
						
	    $this->set('urlstr', $urlstr);
		
	    $this->set('compeld', $compeld);
	    $this->set('webroot', $webroot);
	    $this->set('app', $app);
	    $this->set('act', $act);
	
        $this->set('pagename', $pagename);			
        $this->set('strtempdata', $strtempdata);	

    }

	
	

    function viewmanagetableuseredit()
    {	
        $changeClassName = @include(CONFIGS . DS . 'tableTag.php');	
		
        if(!isset(Request::$query['dopost']))Request::$query['dopost']='';
	    $action = Request::$query['dopost'];
 	    if($action=='post')
	    {
		    $pagename = $this->_urldecode($_POST['pagename']);		 
		    $strtempdata = $_POST['strtempdata'];		 
		    $fromurl = $this->_urldecode($_POST['fromurl']); 
		    $compeld = $this->_urldecode($_POST['compeld']); 
		    $webroot = $this->_urldecode($_POST['webroot']); 
		    $app = $_POST['app'];		 
		    $act = $_POST['act'];
		    $filedlist = trim($_POST['filedlist']);			
			
	        $fids = Request::get_args('fids');
	        $cids = Request::get_args('cids');			
            $dorder = Request::get_args('dorder');
            $dconditions = Request::get_args('dconditions');
            $userorder = '';
            $userconditions = '';			
            if(!empty($dorder)){
		        foreach($dorder as $key => $value) {
				    if($value!='0') {
		                $userorder .= $key.' '.$value.','; 					
					}
		        }
                $userorder = substr($userorder,0,-1);					
			}
            if(!empty($dconditions)){
		        foreach($dconditions as $key => $value) {
				    if($value!='0') {
		                $userconditions .= $key.' and '; 					
					}
		        }
                $userconditions = substr($userconditions,0,-4);					
			}		 		 
				 
				 
	        $data = $_POST['data'];
            unset($data['dopost']);			
	        $fenorxin = $_POST['fenorxin'];
	        $limitnum = $_POST['limitnum'];
			
		    if(empty($limitnum)) {
		        $limitnum = 10;
		    }
		 		 
		    $strdata = $this->filtrate($strtempdata);	 
            $this->icon_to_array($strdata);
	        $old_icondata = $this->icon_to_array;


		    $old_icondata['fields'] = $filedlist;
		    $old_icondata['limit']=$limitnum;			 	

			if($fenorxin=='1') {
				$temp_table_filed_id = array();
				if(isset($cids[0])) {
				    $conditions = $userconditions;	
					$temp_fids_array = filterEmptyVar(explode(",",$cids[0]));
					if(!isset($temp_fids_array[0]))$temp_fids_array[0]='';
					if(!isset($temp_fids_array[1]))$temp_fids_array[1]='';			   
					$temp_table_filed_id['tablename'] = $temp_fids_array[0];
					$temp_table_filed_id['filedname'] = $temp_fids_array[1];
					$temp_table_filed_id['id'] = '';
					foreach($cids as $key=>$value) {
						$temp_fids_array = filterEmptyVar(explode(",",$value));			   
						if(!isset($temp_fids_array[2]))$temp_fids_array[2]='';
                        if(!empty($temp_fids_array[2]))	$temp_table_filed_id['id'] .= $temp_fids_array[2].',';							   			   			    
					}					
					if(!empty($temp_table_filed_id['id'])) {
					    $temp_table_filed_id['id'] = substr($temp_table_filed_id['id'],0,-1);
					}
					
					$old_icondata['tablename'] = $temp_table_filed_id['tablename'];	
					$changevalue = array();			
					if(!empty($temp_table_filed_id['tablename'])&&!empty($temp_table_filed_id['filedname'])){     
						foreach($changeClassName as $value) {
							if($value['table']==$temp_table_filed_id['tablename'] && ($value['filed1'].'|'.$value['filed2'])==$temp_table_filed_id['filedname'] ) {
								$changevalue = $value;
							}
						}				
						if(isset($temp_table_filed_id['id'])&&!empty($temp_table_filed_id['id']) && !empty($changevalue)){
						    if(empty($conditions))$conditions .=' 1 ';
							$conditions .= ' and '.$changevalue['lconditions'].' in('.$temp_table_filed_id['id'].') ' ;
						}					
					}					
			        $old_icondata['conditions']= $conditions;
			        $old_icondata['order']= $userorder;						
			        $old_icondata['limit']= $limitnum;	
			        $old_icondata['pageurl']= '';
                }					
			}else {
				$temp_table_filed_id = array();
				if(isset($fids[0])) {			
					$temp_fids_array = filterEmptyVar(explode(",",$fids[0]));
					if(!isset($temp_fids_array[0]))$temp_fids_array[0]='';
					if(!isset($temp_fids_array[1]))$temp_fids_array[1]='';			   
					$temp_table_filed_id['tablename'] = $temp_fids_array[0];
					$temp_table_filed_id['filedname'] = $temp_fids_array[1];
					$temp_table_filed_id['id'] = '';
					foreach($fids as $key=>$value) {
						$temp_fids_array = filterEmptyVar(explode(",",$value));			   
						if(!isset($temp_fids_array[2]))$temp_fids_array[2]='';	   	
                        if(!empty($temp_fids_array[2]))	$temp_table_filed_id['id'] .= $temp_fids_array[2].',';							
					}
					if(!empty($temp_table_filed_id['id'])) {
					    $temp_table_filed_id['id'] = substr($temp_table_filed_id['id'],0,-1);
					}
					if(!empty($temp_table_filed_id['id'])) {
					    $conditions = ' '.$temp_table_filed_id['filedname'].' in('.$temp_table_filed_id['id'].')';
					}					
					$old_icondata['tablename'] = $temp_table_filed_id['tablename'];							
			        $old_icondata['conditions']=$conditions;
			        $old_icondata['order']='';				
			        $old_icondata['limit']='';			
			    }
			}
			
			
            $iconcontent = '';
		    foreach($old_icondata as $key => $value) {
		        $iconcontent .= '  '.$key."=\'".$value."\'";
		    }			 	  	
      
		    $iconcontent = str_replace(array('&quot;','&#39;','\\'),array('"',"'",''),$iconcontent);			


		    $filename = $webroot . DS .$pagename;
		    $savefilename = $compeld;
		    $savedir = str_replace($pagename ,''  ,$savefilename);				
			
		    $tempfilename = '';				
		    if(is_file($savefilename) && @ filemtime($savefilename) > @ filemtime($filename))
		    {			
		        $tempfilename = $savefilename;				   			
		    } else {
		        $tempfilename = $filename;					
		    }		
		    $fileContent = "";
		    if (!($fileContent = file_get_contents($tempfilename))) {
				return false;
		    }
				
		    $fileContent = preg_replace('/^(\xef\xbb\xbf)/', '', $fileContent); //EFBBBF
		    $fileContent = str_replace($strdata, $iconcontent, $fileContent);
		
		    if (!file_exists($savefilename)) {
                $CviewFileName = str_replace(array('\\','/'), DS ,$savefilename);
				$temp = explode(DS, $CviewFileName);
				array_pop( $temp );
                $tempFileDir = implode( DS.'' , $temp );
				get_dir($tempFileDir);
		    }
		
		    if (!file_put_contents($savefilename,  $fileContent)) {
			    return false;
		    }
		    echo '<script >alert("修改成功!");parent.location.reload();</script>';
		    exit;
/* 		    $notice = &get_message(array('msg'=>'修改成功','url'=>$this->_urldecode($fromurl),'title'=>'Notice'));
		    $notice ->display();	 */
   
	    } 
		   
	   			 	   
        if($action=='getfidsonslist')
	    { 
	        $fup = Request::$query['fup'];
		    $class = Request::$query['class'];
		    $ctype = Request::$query['ctype'];
		    $page =  Request::$query['page'];
		    $keyword =  Request::$query['keyword'];				
		    $Murl = $this->base.'/index.php/tagsedit/viewmanagetableuseredit/';
			

	        $tablearray = array();

		    $DbConfig = 'default';
    		
	        $db =& ConnectionManager::getDataSource($DbConfig);
		    $DbPrefix = $db->config['prefix'];		
		
	        $temparray = $db->listSources();
		    foreach($temparray as $key => $value)
		    {
			    if($value)
			    {
		            $temp_str = str_replace($DbPrefix,'',$value);
				    $temp_array = $db->TableDescribe($value);
 		            $tablearray[$value][$value] = $temp_str; 

		            foreach($temp_array as $key1=>$value1)
				    {
			            $tablearray[$value][$value.'.'.$value1] = $temp_str.'.'.$value1;

			        }
			    }

		    }			
			  
            header('Content-type: text/html; charset='.Param::read('App.encoding'));	
		  
  		    $tables_fileds = filterEmptyVar(explode(",",$fup));

		    $tablename_array = array();
		    $fields_array = array();
		    $temp_fields_array = array();
		    $temp_fields_array1 = array();		 		 
            $data = array();
		 	 
            if(isset($tables_fileds[0])&&!empty($tables_fileds[0]))
		    {
		        $temp = $temp1 = array();	
		        $temp = explode("|",$tables_fileds[0]);			
                $temp = filterEmptyVar($temp);
			    $num=1;
                foreach($temp as $key => $value)
			    {
		            $temp1 = explode(".",$value);
				    if(isset($temp1[1]))
				    {
				        $tablename_array[$num]['table'] = $temp1[0];
				        $tablename_array[$num]['field'] = $temp1[1];
				    } else {
				        $tablename_array[$num]['table'] = $temp1[0];
				        $tablename_array[$num]['field'] = 0;			
				    }
				    $num++;
			    }

		    }
	
            if(isset($tables_fileds[1])&&!empty($tables_fileds[1]))
		    {
		        $temp = $temp1 = array();	
		        $temp = explode("|",$tables_fileds[1]);			
                $temp = filterEmptyVar($temp);
			
                foreach($temp as $key => $value)
			    {
		            $temp1 = explode(":",$value);
				    if(isset($temp1[1]))
				    {
				        $fields_array[$temp1[0]] = $temp1[1];
				    }else{
				        $fields_array[$temp1[0]] = 0;				
				    }
				    $temp3 = explode(".",$temp1[0]);
                    $temp_fields_array1[$key] = $temp1[0];								
                    $temp_fields_array[$key] = $temp3[1];				
			    }
		    }


            $changevalue = array();
		    if(isset($tables_fileds[0])&&isset($tables_fileds[1])&&!empty($tables_fileds[0])&&!empty($tables_fileds[1])){     
			    $temp_on = '';
			    $table_pre_key = '';
			    $table_pre_value = '';
				$conditions =' 1 ';
				
				foreach($changeClassName as $value) {
					if($value['table']==$tables_fileds[0] && ($value['filed1'].'|'.$value['filed2'])==$tables_fileds[1] ) {
						$changevalue = $value;
				    }
				}				
                if(isset($tables_fileds[2])&&!empty($tables_fileds[2])){
					if(!empty($changevalue)) {
					    $conditions .= ' and '.$changevalue['lconditions'].'='.$tables_fileds[2].' ' ;
					}
				}				
                if(isset($changevalue['conditions']) && !empty($changevalue['conditions'])) {
                    $conditions .= ' and '.$changevalue['conditions'];
                }	
				if(!empty($keyword) && isset($temp_fields_array1[1])) {
                    $conditions .= ' and '.$temp_fields_array1[1].' like"%'.$keyword.'%"';				     
				}				
                if(count($tablename_array)==1)
			    {
			        $tables = $DbPrefix.$tablename_array[1]['table']. ' '.$tablename_array[1]['table'];
			    } else {
                    for($num=1;$num<=count($tablename_array);$num++)
		            {
				        if(empty($tablename_array[$num]['field'])){$tablename_array[$num]['field'] = ' ';}

                        if(isset($tablename_array[$num+1])&&$num==1) {	

                            $tables.= $DbPrefix.$tablename_array[$num]['table'] . ' '.$tablename_array[$num]['table'].' LEFT JOIN '.$DbPrefix.$tablename_array[$num+1]['table'] . ' '.$tablename_array[$num+1]['table'].' ON '. $tablename_array[$num]['table'] .'.'.$tablename_array[$num]['field'].'='. $tablename_array[$num+1]['table'] .'.'.$tablename_array[$num+1]['field'].' ';
				  
				        } else {   
					        if(isset($tablename_array[$num+1]))
					        {
	                          $tables.= ' LEFT JOIN '.$DbPrefix.$tablename_array[$num+1]['table'] . ' '.$tablename_array[$num+1]['table'].' ON '. $tablename_array[$num]['table'] .'.'.$tablename_array[$num]['field'].'='. $tablename_array[$num+1]['table'] .'.'.$tablename_array[$num+1]['field'].' ';						  
					        }
		  
				        }
		            }	
			    }

                foreach($fields_array as $key => $value)
		        {
			        if(empty($value))
			        {
                        $fields.= $key . ' ,';			
			        }else {
                        $fields.= $key . ' as '.$value.' ,';			
			        }
		        }
  			    $fields = substr($fields, 0,-1);


                $data = $this->TagsModel(array("table" => $tables,"fields" => $fields,"conditions" => $conditions,"order" => '',"limit" => '20',"page" => $page));  
		    }

             $user_order = "";
            $user_conditions = "";			
		    if(!empty($changevalue)) {
			    if(isset($changevalue['userorder']) && !empty($changevalue['userorder']) ) {				   
					foreach($changevalue['userorder'] as $key1 => $value1)
					{			             
						$user_order .= $value1.':<select name="dorder['.$key1.']"><option  value="0">不排序</option><option  value="asc">从高到低</option><option  value="desc">从低到高</option></select>&nbsp;&nbsp;';	   
					}				   
				}	
			    if(isset($changevalue['userconditions']) && !empty($changevalue['userconditions']) ) {				   
					foreach($changevalue['userconditions'] as $key1 => $value1)
					{			             
						$user_conditions .= '<input type="checkbox" name="dconditions['.$key1.']" value="1" >'.$value1.'&nbsp;&nbsp;';	   
					}				   
				}				
			}          
		   
		    $select_str = "";

		    foreach($tablename_array as $key => $value)
		    {
		    
		        foreach($tablearray[$DbPrefix.$value['table']] as $key1 => $value1)
		        {			             
                    $select_str .= '<option  value="'.$value1.'">&nbsp;|&nbsp;&nbsp;&nbsp;'.$key1.'</option>';	   
		        }
			    $select_str .= '<optgroup label="===================" style="color:red;"></optgroup>';	 

		    }
		 		 

		 
            $str='';
			$str.= "<div style=\'color:red;\'>关键字:<input type=\'text\' class=\'txt\' name=\'keyword\' value=\'".$keyword."\' style=\'width:150px;height=30px;\' onchange=\"changekeyword(1,this,\'".$fup."\',true,0 ,\'$Murl\',\'1\')\" ></div>"; 						
		    foreach($data['data'] as $key => $value)
		    {
		        $next='false';
		        $icon_fup = true;
				if($value[$temp_fields_array[0]])
				{
				$str.= "<div><a href=\"javascript:;\" onclick=\"changeClassName($class,this,\'".$tables_fileds[0].','.$temp_fields_array1[0].','.$value[$temp_fields_array[0]]."\',$next,$ctype ,\'$Murl\')\">".'['.$value[$temp_fields_array[0]].']'.$value[$temp_fields_array[1]]."</a></div>"; 				
				}
	
			   
		    }
			if(!empty($data['data'])) {			
			$str.= "<div><a style=\'color:red;\' href=\"javascript:;\" onclick=\"changeClassName(1,this,\'".$fup."\',true,0 ,\'$Murl\',\'".($data['page']['current']['num']-1)."\',\'$keyword\')\">上一页[第".$data['page']['current']['num']."页,共".$data['page']['pageCount']."页]</a></div>"; 	
			$str.= "<div><a style=\'color:red;\' href=\"javascript:;\" onclick=\"changeClassName(1,this,\'".$fup."\',true,0 ,\'$Murl\',\'".($data['page']['current']['num']+1)."\',\'$keyword\')\">下一页[共".$data['page']['total']."条]</a></div>"; 			
	        }else {
			$str.= "<div><a style=\'color:red;\' href=\"javascript:;\" onclick=\"changeClassName(1,this,\'".$fup."\',true,0 ,\'$Murl\',\'1\',\'$keyword\')\">首页[第".$data['page']['current']['num']."页,共".$data['page']['pageCount']."页,共".$data['page']['total']."条]</a></div>"; 					
			}			
	        $str=$str?$str:"&nbsp;";
	        echo '<script >parent.inputcontent(\''.$str.'\','.$class.',\''.$select_str.'\',\''.$user_order.'\',\''.$user_conditions.'\');</script>';
	        exit;		
        }
  
 		$iconname = Request::$query['iconname'];
		
		$pagename = Request::$query['pagename'];	
			
		$strdata = Request::$query['data'];
		
		$weburl = Request::$query['weburl'];
		
		$webroot = Request::$query['webroot'];
		
		$act = Request::$query['act'];	
		
		$app = Request::$query['app'];
		
		$compeld = Request::$query['compeld'];		
		
		$compeld= $this->_urldecode($compeld);
		$compeld1 = $compeld;				
		$compeld= $this->_urlencode($compeld);


		$pagename= $this->_urldecode($pagename);
		$pagename1 = $pagename;				
		$pagename= $this->_urlencode($pagename);

		$webroot= $this->_urldecode($webroot);
		$webroot1 = $webroot;				
		$webroot= $this->_urlencode($webroot);		
	
		$weburl= $this->_urldecode($weburl);
		$fromurl = $weburl;				
		$weburl= $this->_urlencode($weburl);
		
		$divwidth = Request::$query['divwidth'];
				
		$divheight = Request::$query['divheight'];
	
		$strtempdata = $strdata;
		$urlstr="&weburl={$weburl}&compeld={$compeld}&app={$app}&act={$act}&webroot={$webroot}&iconname={$iconname}&pagename={$pagename}&data={$strdata}&divwidth={$divwidth}&divheight={$divheight}";


  		
		$strdata = $this->filtrate($strdata);	

		$this->icon_to_array($strdata);
	    $data['config'] = $this->icon_to_array;

		
		
		if(!isset($data['config']['limit'])){
		    $data['config']['limit'] = '';
		}	
		
		$limitnum = intval(trim($data['config']['limit'])) ;


	   
		$data['iconcontent'] = '<psp:tags '.$strdata.' />';
		$data['iconname'] = $iconname;	



		$DbConfig = 'default';
    		
	    $db =& ConnectionManager::getDataSource($DbConfig);
		$DbPrefix = $db->config['prefix'];		
        $changeClassName_data = array();
        foreach($changeClassName as $key => $value ){
  		    if(isset($value['cattable']) && isset($value['catfiled1']) && isset($value['catfiled2']) && isset($value['lconditions'])&& isset($value['catparent']) 
			   && !empty($value['cattable']) && !empty($value['catfiled1']) && !empty($value['catfiled2']) && !empty($value['lconditions']) && !empty($value['catparent'])){
			   if(!isset($value['catconditions']))$value['catconditions']='';
		        $chag_query = "SELECT * FROM ".$DbPrefix.$value['cattable']." ".$value['catconditions'];
		        $result_chag = $db->QueryAll($chag_query);	
                foreach($result_chag as $v) {
	                $changeClassName_data[$key][$v[$value['catparent']]][$v['id']] = $v;
                }				
			}
	    }			
		$code= file_get_contents($webroot1 . DS .'tags' . DS . $data['config']['htmlname'].'.html');
		
		preg_replace("/\<psp\:filedlist\>\s*(.+?)\s*\<\/psp\:filedlist\>/ies", "\$this->_filed_list_code('\\1')", $code);
        if(is_array($this->_filedlistcode) && !empty($this->_filedlistcode) && isset($this->_filedlistcode[$data['config']['tablename']])) {
		    $data['config']['fields'] = $this->_filedlistcode[$data['config']['tablename']];
			$isdo = 1 ;
			foreach($changeClassName as $key => $value) {
			    if($value['table'] == $data['config']['tablename'] ) {
                    $changeClassName = array($value) ;
					$isdo = 0 ;
					break;
                }				
			}
            if($isdo) {
	            $changeClassName = array(
	                array('name' =>'表名为'.$data['config']['tablename'].'的数据',
		                'table' =>$data['config']['tablename'],
			            'filed1' =>$data['config']['tablename'].'.id',
			            'filed2' =>$data['config']['tablename'].'.id',			   
	                ),			  
	            );
            }
	        $changeClassName[] = 
	            array('name' =>'退回数据',
		                'table' =>'',
			            'filed1' =>'',
			            'filed2' =>'',			   
	            );			  			
		}
 	
        $icontabledata = '';
		$tempclass_name = array();
        if(isset($data['config']['tablename'])&&!empty($data['config']['tablename']))
		{		
		    $sql_order ='';
		    $sql_limit = '';
		    $sql_conditions = '';
		    $sql_table = '';
		    $sql_field = '';
			$tableid = '';
			$sql = '';
			
		    					
		    $tablename_array = array();
		    $fields_array = array();
		    $fields_temp_array = array();								
		    $temp = $temp1 = array();	
		    $temp = explode("|",$data['config']['tablename']);			
            $temp = filterEmptyVar($temp);
			$num=1;
            foreach($temp as $key => $value)
			{
		        $temp1 = explode(".",$value);
				if(isset($temp1[1]))
				{
				$tablename_array[$num]['table'] = $temp1[0];
				$tablename_array[$num]['field'] = $temp1[1];
				}
				else
				{
				$tablename_array[$num]['table'] = $temp1[0];
				$tablename_array[$num]['field'] = 0;			
				}
				$num++;
			}
			$isset_changeClassName = false;
            foreach($changeClassName as $key => $value )
			{
  		       if($value['table']==$data['config']['tablename'])
			   {
			     $categorydata =  $value['name'];
				 $tempclass_name = $value;
			     $fields_array[$value['filed1']] = 0;
			     $fields_array[$value['filed2']] = 0;
				 $temp = explode(".",$value['filed1']);
				 $fields_temp_array[1]= $temp[1];
				 $temp = explode(".",$value['filed2']);
				 $fields_temp_array[2]= $temp[1];
			     $isset_changeClassName = true;				 
			   }
			}
			
            if(count($tablename_array)==1)
			{
			   $sql_table = $DbPrefix.$tablename_array[1]['table']. ' '.$tablename_array[1]['table'];
			}
			 else
			{
              for($num=1;$num<=count($tablename_array);$num++)
		      {
				  if(empty($tablename_array[$num]['field'])){$tablename_array[$num]['field'] = ' ';}

                  if(isset($tablename_array[$num+1])&&$num==1)
				  {	

                  $sql_table.= $DbPrefix.$tablename_array[$num]['table'] . ' '.$tablename_array[$num]['table'].' LEFT JOIN '.$DbPrefix.$tablename_array[$num+1]['table'] . ' '.$tablename_array[$num+1]['table'].' ON '. $tablename_array[$num]['table'] .'.'.$tablename_array[$num]['field'].'='. $tablename_array[$num+1]['table'] .'.'.$tablename_array[$num+1]['field'].' ';
				  
				  }
				  else
				  {   
					  if(isset($tablename_array[$num+1]))
					  {
	                  $sql_table.= ' LEFT JOIN '.$DbPrefix.$tablename_array[$num+1]['table'] . ' '.$tablename_array[$num+1]['table'].' ON '. $tablename_array[$num]['table'] .'.'.$tablename_array[$num]['field'].'='. $tablename_array[$num+1]['table'] .'.'.$tablename_array[$num+1]['field'].' ';						  
					  }
		  
				  }


		      }	
			  
			}

            foreach($fields_array as $key => $value)
		    {
			   if(empty($value))
			   {
                $sql_field.= $key . ' ,';			
			   }else
			   {
                $sql_field.= $key . ' as '.$value.' ,';			
			   }
		    }
  			$sql_field = substr($sql_field, 0,-1);
		
			
            if(isset($data['config']['order']))
		    {
		      $sql_order = ''.$data['config']['order'];		
		    }
            if(isset($data['config']['limit']))
		    {
		      $sql_limit = ''.$data['config']['limit'];		
		    }
            if(isset($data['config']['conditions']))
		    {
		      $sql_conditions = ''.$data['config']['conditions'];		
		    }

		
           ($sql_conditions=='')?$sql_conditions = '':$sql_conditions = '  WHERE  '.$sql_conditions;	
            $sql_order && $sql_order = ' ORDER BY ' . $sql_order . ' ';
            $sql_limit && $sql_limit = " LIMIT 0,{$sql_limit} ";
			$tempdata =array();
		    if($isset_changeClassName){			
		    $query = "SELECT {$sql_field} FROM {$sql_table} {$sql_conditions} {$sql_order} {$sql_limit}";
		    $tempdata = $db->QueryAll($query);			
			}
			foreach($tempdata as $key => $value)
			{
			    if(isset($value[$fields_temp_array[1]])&&!empty($value[$fields_temp_array[1]]))
				{
			    $icontabledata .= '<div id="div_'.$tempclass_name['table'].','.$tempclass_name['filed1'].','.$value[$fields_temp_array[1]].'"><input type="checkbox" name="fids[]" value="'.$tempclass_name['table'].','.$tempclass_name['filed1'].','.$value[$fields_temp_array[1]].'" checked="" onclick="removeFid(this);">  <font color="#232323"> &gt; '.$categorydata.' &gt; ['.$value[$fields_temp_array[1]].']'.$value[$fields_temp_array[2]].'</font></div>';				
				}else
				{
				$icontabledata .= '';
				}
			
			}       
		}
		$is_fenorxin = 1;
		if(!empty($tempclass_name) && isset($data['config']['conditions'])&&!empty($data['config']['conditions'])&& strstr($data['config']['conditions'],$tempclass_name['filed1']))
		{
			$is_fenorxin = 0;
		}
		$conitions_catids = array();//得到条件属性中的分类id号
		if(!empty($tempclass_name) && isset($data['config']['conditions'])&&!empty($data['config']['conditions'])&& strstr($data['config']['conditions'],$tempclass_name['lconditions'])) {
		    $temp_catids = explode($tempclass_name['lconditions'],$data['config']['conditions']);
			foreach($temp_catids as $v) {
				$v = trim($v);
				if(strpos($v,'=')===0){
					$v = trim(str_replace('=','',$v));
					$t = explode(' ',$v);
					if(isset($t[0])&& !empty($t[0])) {
						$t = trim($t[0]);
						$conitions_catids[$t] = $t;			
					}
				}else if(strpos($v,'in')===0) {
					$v = trim(str_replace('in','',$v));		
					if(strpos($v,'(')===0) {
						$v = trim(str_replace('(','',$v));
						$t = explode(')',$v);
						if(isset($t[0])&& !empty($t[0])) {
							$t = trim($t[0]);
							if(!empty($t)) {
								$t = explode(',',$t);
								foreach($t as $v1) {
									$conitions_catids[$v1] = $v1;	
								}
							}
									
						}			
					}
				}
			}
			if(!empty($conitions_catids)) {
			    foreach($conitions_catids as $v) {
				    $conitions_catids[$tempclass_name['table'].','.$tempclass_name['filed1'].'|'.$tempclass_name['filed2'].','.$v] = $tempclass_name['table'].','.$tempclass_name['filed1'].'|'.$tempclass_name['filed2'].','.$v;
				}
			}
		}
        $this->set('conitions_catids', $conitions_catids);			

		$user_order = "";
		$user_conditions = "";
		if(!empty($tempclass_name)) {
			if(isset($tempclass_name['userorder']) && !empty($tempclass_name['userorder']) ) {	
                if(!isset($data['config']['order']))$data['config']['order'] = '';			
				$ot = explode(",",$data['config']['order']);				
				foreach($tempclass_name['userorder'] as $key => $value)
				{	
					$otis = '0';
					foreach($ot as $v) {
						if(strstr($v,$key)) {
							$otis = trim(str_replace($key,'',$v));
						}
					}						
					$user_order .= $value.':<select name="dorder['.$key.']">';
					$user_order .= '<option  value="0" '.(($otis=='0')?'selected':'').'>不排序</option>';
					$user_order .= '<option  value="asc" '.(($otis=='asc')?'selected':'').'>从高到低</option>';
					$user_order .= '<option  value="desc" '.(($otis=='desc')?'selected':'').'>从低到高</option></select>&nbsp;&nbsp;';	   
				}				   
			}			    

			if(isset($tempclass_name['userconditions']) && !empty($tempclass_name['userconditions']) ) {
			    if(!isset($data['config']['conditions']))$data['config']['conditions'] = '';			
				$ot = $data['config']['conditions'];					
				foreach($tempclass_name['userconditions'] as $key => $value)
				{	
					$otis = '';
					if(strstr($ot,$key)) {
						$otis = ' checked ';
					}
					$user_conditions .= '<input type="checkbox" name="dconditions['.$key.']" value="1" '.$otis.' >'.$value.'&nbsp;&nbsp;';	   
				}				   
			}				

		}
        $this->set('user_order', $user_order);		
        $this->set('user_conditions', $user_conditions);		
		
		
		$relativepath = str_replace(APP_ROOT,'',$webroot1 . DS .'tags');
		
		$relativepath = str_replace(DS,'/',$relativepath);
		
        $yuanrelativepath = $relativepath .'/';		
        $bianrelativepath = str_replace('/','\/',$yuanrelativepath);		
			
        $this->set('yuanrelativepath', $yuanrelativepath);		
        $this->set('bianrelativepath', $bianrelativepath);
		
        $this->set('relativepath', $relativepath);
		
        $this->set('is_fenorxin', $is_fenorxin);		
		
        $this->set('is_fenorxin', $is_fenorxin);		
        $this->set('icontabledata', $icontabledata);
	

        $this->set('htmlname', $data['config']['htmlname']);
				
        $this->set('changeClassName', $changeClassName);
        $this->set('changeClassName_data', $changeClassName_data);			
		if(!isset($tablename_array)){
		    $tablename_array = array();
		}
		if(!isset($data['config']['tablename'])){
		    $data['config']['tablename'] = '';
		}			
        $this->set('tablename_array', $tablename_array);
				
        $this->set('tablename', $DbPrefix.$data['config']['tablename']);
		     
	    $this->set('limitnum', $limitnum);
		
	    $this->set('data', $data);
	    $this->set('weburl', $weburl);
	    $this->set('fromurl', $fromurl);	
						
	    $this->set('urlstr', $urlstr);
		
	    $this->set('compeld', $compeld);
	    $this->set('webroot', $webroot);
	    $this->set('app', $app);
	    $this->set('act', $act);
	
        $this->set('pagename', $pagename);			
        $this->set('strtempdata', $strtempdata);	

    }	
	

    function viewmanagecacheedit()
    {
	    if(isset(Request::$query['webroot'])) {
		    $webroot = Request::$query['webroot'];
		    $webroot= $this->_urldecode($webroot);
		    $webroot1 = $webroot;				
		    $webroot= $this->_urlencode($webroot);			
		}else {		    
		    $webroot = $this->_urldecode($_POST['webroot']);
		    $webroot1 = $webroot;			
		}
		$relativepath = str_replace(APP_ROOT,'',$webroot1 . DS .'tags');
		$relativepath = str_replace(DS,'/',$relativepath);
		$relativepath_array = filterEmptyVar(explode('/', $relativepath));
        $relativepath1 = implode('/', $relativepath_array);

	

        $changeClassName = array();
        $edit_url = '';


        $cachearray = array();
/*		
        $cachearray['app/config/'] = $this->get_all_dirname(APP_ROOT . DS . 'app/config');
        $cachearray['app/temp/'] = $this->get_all_dirname(APP_ROOT . DS .  'app/temp');
		foreach($cachearray as $key =>$value)
		{  	   
		   if($key == 'app/config/')
		   {
		        foreach($value as $key1 =>$value1)
		       {
		        $changeClassName[] = array('name' =>'config'.$key1,
		                                   'alias' =>'ConfigData['.$value1.']',
		                                   'type' =>'cache',
			                               'dir' =>$key.$value1,//这个要相对根目录的路径
			                               'fileds' =>'',
			                               'showfileds1' =>'name',
			                               'showfileds2' =>'link',			   			   			   
	                                       );
		       }
			  		   
		   
		   }
		   else if($key == 'app/temp/')
		   {
		        foreach($value as $key1 =>$value1)
		       {
			         if(strstr($value1,'links'))
		             {
		                $changeClassName[] = array('name' =>'links'.$key1,
		                                   'alias' =>'友情链接['.$value1.']',
		                                   'type' =>'cache',
			                               'dir' =>$key.$value1,//这个要相对根目录的路径
			                               'fileds' =>'',
			                               'showfileds1' =>'name',
			                               'showfileds2' =>'url',			   			   			   
	                                       );
										   

		             }else if (strstr($value1,'rollpic'))
					 {
		                $changeClassName[] = array('name' =>'links'.$key1,
		                                   'alias' =>'滚动图片['.$value1.']',
		                                   'type' =>'cache',
			                               'dir' =>$key.$value1,//这个要相对根目录的路径
			                               'fileds' =>'',
			                               'showfileds1' =>'name',
			                               'showfileds2' =>'images_url',			   			   			   
	                                       );
										   					 
					 }

		       }		   
		   
		   }
		   
		}	 */		
		
	



	
        $temparray = array();
        $temparray = $this->get_all_dirname($webroot1 . DS .'tags' . DS . 'tempdata');
	    foreach($temparray as $key => $value)
		{
		   if(strpos($value,'.tags')) {
		    $icondataarray[str_replace('.tags','',$value)] = $value;
		   }
		}		
			        
        $icondataarray = array();
        $temparray = array();
        $temparray = $this->get_all_dirname($webroot1 . DS .'tags' . DS . 'tempdata');
	    foreach($temparray as $key => $value)
		{
		   if(strpos($value,'.tags'))
		   {
		    $temp_data = include($webroot1 . DS .'tags' . DS . 'tempdata' .DS.$value);
		    $icondataarray[str_replace('.tags','',$value)]['type'] = $temp_data['type'];
		    $icondataarray[str_replace('.tags','',$value)]['description'] = $temp_data['description'];						
		    $icondataarray[str_replace('.tags','',$value)]['file'] = $value;
			unset($temp_data);
		   }
		}

		foreach($icondataarray as $key =>$value)
		{  
		   if($value['type'] == 'data') {
		        $changeClassName[] = array('name' =>$key,
		                                   'alias' =>$value['description'],
		                                   'type' =>'tags',
			                               'dir' =>$relativepath1.'/'.'tempdata'.'/'.$value['file'],/*这个要相对根目录的路径*/
			                               'fileds' =>'',
			                               'showfileds1' =>'datakey',
			                               'showfileds2' =>'datavalue',			   			   			   
	                                       );	   
		        
		   }		   
		   if($value['type'] == 'file') {
		        $changeClassName[] = array('name' =>$key,
		                                   'alias' =>$value['description'],
		                                   'type' =>'tags',
			                               'dir' =>$relativepath1.'/'.'tempdata'.'/'.$value['file'],/*这个要相对根目录的路径*/
			                               'fileds' =>'',
			                               'showfileds1' =>'upfile',
			                               'showfileds2' =>'description',			   			   			   
	                                       );	   
		   
		   }	
		   if($value['type'] == 'fileworld') {
		        $changeClassName[] = array('name' =>$key,
		                                   'alias' =>$value['description'],
		                                   'type' =>'tags',
			                               'dir' =>$relativepath1.'/'.'tempdata'.'/'.$value['file'],/*这个要相对根目录的路径*/
			                               'fileds' =>'',
			                               'showfileds1' =>'upfile',
			                               'showfileds2' =>'title',			   			   			   
	                                       );	   
		   
		   }			   
		   
		}	

	
	    $action = Request::get_args('dopost');
	   if($action=='post')
	   {
		    $pagename = $this->_urldecode($_POST['pagename']);		 
		    $strtempdata = $_POST['strtempdata'];		 
		    $fromurl = $this->_urldecode($_POST['fromurl']); 
		    $compeld = $this->_urldecode($_POST['compeld']); 
		    $webroot = $this->_urldecode($_POST['webroot']); 
		    $app = $_POST['app'];		 
		    $act = $_POST['act'];		   
	   
	        $fids = $_POST['fids'];
		 
	        $tplpath = $_POST['tplpath'];		 
	        $tplpart_1 = $_POST['tplpart_1'];		 
	        $data = $_POST['data'];
            unset($data['dopost']);	
	        $mokuai = $_POST['mokuai'];


		 
		    $strdata = $this->filtrate($strtempdata);	 
            $this->icon_to_array($strdata);
	        $old_icondata = $this->icon_to_array;

		 $fields_str = '';
		 if(isset($data)&&!empty($data))
		 {
		   foreach($data as $key => $value)
		   {
		     $fields_str .= $value['datavalue'].':'.$value['datakey'].'|'; 
		   }
		 }


	     $cur_changeclassname = array();

		 foreach($changeClassName as $key => $value)
		 {
			 if($value['name'] == $fids[0])
			 {
		         $cur_changeclassname = $value;
			 }
		 }

		 if($cur_changeclassname['type']=='tags')
		 {
		    $old_icondata['iconfields'] = $fields_str;
			$file_name = basename($cur_changeclassname['dir']);
		    if(strpos($file_name,'.tags'))
		    {
		      $old_icondata['icondata'] = str_replace('.tags','',$file_name);
		    }			
					  
		 }else
		 {
		    $old_icondata['cachedatadir'] = $cur_changeclassname['dir'];	
		    $old_icondata['cachefields'] = $fields_str;					  
		 }		 
		 
		 $old_icondata['htmlname'] = str_replace('.html','',basename($tplpath));
			 	
         if($mokuai==1)
		 {
  		    $temp_html_name = filterEmptyVar(explode(".",$old_icondata['htmlname']));
		    $old_icondata['htmlname'] = $temp_html_name[0];			
				$icon_dir = $webroot . DS . 'tags';
		    $iconfile = $old_icondata['htmlname'].'.'.time().'.html';
		    $iconfilename = $old_icondata['htmlname'].'.'.time().'';
		    $old_icondata['htmlname'] = $iconfilename;			
	        $temparray = $this->get_all_dirname($icon_dir);
            $is = 1;
	        foreach($temparray as $key => $value)
		    {
		      if($value==$iconfile)
		      {
              $is = 0;break;
		      }	
            }
			
		    if($is)
		    {
                $iconhtml = stripslashes($tplpart_1);
		        $iconhtml = str_replace(array('&quot;','&#39;'),array('"',"'"),$iconhtml);				
                $iconhtmlarray = @include($icon_dir . DS .'tags.php');
		        $iconhtmlarray[$iconfilename]['name'] =$iconfilename;
                $iconhtmlarray[$iconfilename]['html'] =$iconhtml;
				$iconhtmlarray[$iconfilename]['config'] =array();
					if (!(file_put_contents($icon_dir . DS .'tags.php', "<?php \nreturn " . var_export($iconhtmlarray , true) . ";\n?>")
					     && file_put_contents($icon_dir.DS.$iconfile,$iconhtml))
						) {
			            return false;
		            }	
		    }
		    else
		    {		
                exit('Icondata名称有相同，请重新提交');				
		    }
		     
		 }else if ($mokuai==0)
		 {

		        $icon_dir = $webroot . DS . 'tags';
		        $iconfile = $old_icondata['htmlname'].'.html';
		        $iconfilename = $old_icondata['htmlname'];		 
                $iconhtml = stripslashes($tplpart_1);
		        $iconhtml = str_replace(array('&quot;','&#39;'),array('"',"'"),$iconhtml);				
                $iconhtmlarray = @include($icon_dir . DS .'tags.php');
		        $iconhtmlarray[$iconfilename]['name'] =$iconfilename;
                $iconhtmlarray[$iconfilename]['html'] =$iconhtml;
				$iconhtmlarray[$iconfilename]['config'] =array();
				if (!(file_put_contents($icon_dir . DS .'tags.php', "<?php \nreturn " . var_export($iconhtmlarray , true) . ";\n?>")
					     && file_put_contents($icon_dir.DS.$iconfile,$iconhtml))
					) {
			        return false;
		        }				 
		 }
        $iconcontent = '';
		foreach($old_icondata as $key => $value)
		{
		   $iconcontent .= '  '.$key."=\'".$value."\'";
		}			 	  	

		$iconcontent = str_replace(array('&quot;','&#39;','\\'),array('"',"'",''),$iconcontent);			

		    $filename = $webroot . DS .$pagename;
		    $savefilename = $compeld;
		    $savedir = str_replace($pagename ,''  ,$savefilename);				
			
		$tempfilename = '';				
		if(is_file($savefilename) && @ filemtime($savefilename) > @ filemtime($filename))
		{			
		     $tempfilename = $savefilename;				   			
		}
		else
		{
		     $tempfilename = $filename;					
		}		
		$fileContent = "";
		if (!($fileContent = file_get_contents($tempfilename))) 
		{
				return false;
		}
				
		$fileContent = preg_replace('/^(\xef\xbb\xbf)/', '', $fileContent); //EFBBBF
		$fileContent = str_replace($strdata, $iconcontent, $fileContent);
		
		if (!file_exists($savefilename)) {
                $CviewFileName = str_replace(array('\\','/'), DS ,$savefilename);
				$temp = explode(DS, $CviewFileName);
				array_pop( $temp );
                $tempFileDir = implode( DS.'' , $temp );
				get_dir($tempFileDir);
		}
		
		if (!file_put_contents($savefilename,  $fileContent)) {
			return false;
		}
		echo '<script >alert("修改成功!");parent.location.reload();</script>';
		exit;
/* 		$notice = &get_message(array('msg'=>'修改成功','url'=>$this->_urldecode($fromurl),'title'=>'Notice'));
		$notice ->display();	 */
   
	   }
       if($action=="getTplcode")
	   {
		
	        $divid = Request::$query['divid'];	
	        $filepath = Request::$query['filepath'];

   		   $filepath = str_replace($this->base,'',$filepath);

           $filepath =str_replace(DS,'/', APP_ROOT.$filepath);	 	
	       $code =  file_get_contents($filepath);
		    $code= str_replace(array("\r","\n","'"),array('\r','\n',"\'"),$code );
    	    echo"<SCRIPT LANGUAGE=\"JavaScript\">
	            <!--
	            parent.puthtmlcode('$code');
	            //-->
	            </SCRIPT>";
	        exit;		
		
		
       }	   
	   			 	   
        if($action=='getfidsonslist'){ 
	        $fup = trim(Request::$query['fup']);
		    $class = Request::$query['class'];
		    $ctype = Request::$query['ctype'];
		    $page =  Request::$query['page'];
		    $Murl = $this->base.'/index.php/tagsedit/viewmanagecacheedit/';
			
	      $temparray = array();
	      $temparray1 = array();
		   $ttt = 'tt';
		  foreach($changeClassName as $key => $value)
		  {
 			if(trim($value['name']) == $fup)
			{
		        $temparray = $value;
			} 

		  }
		  
		  if($temparray['type']=='tags')
		  {
          $temparray1 = include(APP_ROOT .DS .$temparray['dir']);	
		  $temparray1 = $temparray1['data'];	  
		  }else
		  {
          $temparray1 = include(APP_ROOT .DS  .$temparray['dir']);			  
		  }	  
          header('Content-type: text/html; charset='.Param::read('App.encoding'));

		 $select_str = "";		 
		 foreach($temparray1 as $key => $value)
		 {  
		    if(is_array($value)&&!empty($value))
			{
			    foreach($value as $key1 => $value1)
				{
				     if(is_array($value1)&&!empty($value1))
					 {
					     foreach($value1 as $key2 => $value2)
				         {
                             $select_str .= '<option  value="'.$key1.'.'.$key2.'">&nbsp;|&nbsp;&nbsp;&nbsp;'.$key1.'.'.$key2.'</option>';					 
				         } 
					 }else
					 {
                       $select_str .= '<option  value="'.$key1.'">&nbsp;|&nbsp;&nbsp;&nbsp;'.$key1.'</option>';					 
					 }
				}
			}
			break;          
	   
		 }
 
          $str='';
		  if(is_array($temparray1)&&!empty($temparray1))
		  {
		        $next='false';
		        $icon_fup = true;
				$temp_str = ''; 
				foreach($temparray1 as $key => $value)
				{
				    $temp_str .= '['.$key .']'.$value[$temparray['showfileds1']] .'=>'.$value[$temparray['showfileds2']].'<br/>';					
				}
			    if($temparray['type']=='tags')
			    {
			    $temp_str .= '<a href="'.$this->base.'/index.php/tagsedit/icondataedit/?id='.$temparray['name'].'&webroot='.$webroot.'" target="_blank"><font color="red">[edit]</font></a>';	
			    }				
				$str.= "<div><a href=\"javascript:;\" onclick=\"changeClassName($class,this,\'".$fup."\',$next,$ctype ,\'$Murl\')\">(".$temp_str.")</a></div>";			   
		  }			  
	     $str=$str?$str:"&nbsp;";
	     echo '<script >parent.inputcontent(\''.$str.'\','.$class.',\''.$select_str.'\');</script>';
	     exit;	
        }		
	
 		$iconname = Request::$query['iconname'];
		
		$pagename = Request::$query['pagename'];	
			
		$strdata = Request::$query['data'];
		
		$weburl = Request::$query['weburl'];
		
		$act = Request::$query['act'];	
		
		$app = Request::$query['app'];
		
		$compeld = Request::$query['compeld'];		
		
		$compeld= $this->_urldecode($compeld);
		$compeld1 = $compeld;				
		$compeld= $this->_urlencode($compeld);


		$pagename= $this->_urldecode($pagename);
		$pagename1 = $pagename;				
		$pagename= $this->_urlencode($pagename);		
	
		$weburl= $this->_urldecode($weburl);
		$fromurl = $weburl;				
		$weburl= $this->_urlencode($weburl);
		
		$divwidth = Request::$query['divwidth'];
				
		$divheight = Request::$query['divheight'];
	
		$strtempdata = $strdata;
		$urlstr="&weburl={$weburl}&compeld={$compeld}&app={$app}&act={$act}&webroot={$webroot}&iconname={$iconname}&pagename={$pagename}&data={$strdata}&divwidth={$divwidth}&divheight={$divheight}";

		$strdata = $this->filtrate($strdata);	

		$this->icon_to_array($strdata);
	    $data['config'] = $this->icon_to_array;

		$cachedir = '';
		$cachefields = '';			
        if(isset($data['config']['icondata'])&&!empty($data['config']['icondata']))
		{
		
		  $cachedir = $relativepath1.'/'.'tempdata'.'/'.$data['config']['icondata'].'.tags';
		  $cachefields = $data['config']['iconfields'];		  
		}
        if(isset($data['config']['cachedatadir'])&&!empty($data['config']['cachedatadir']))
		{
		  $cachedir = $data['config']['cachedatadir'];
		  $cachefields = $data['config']['cachefields'];		  
		}		
				
        $iconfield =  filterEmptyVar(explode("|",$cachefields));
		$iconfielddata = array();
        foreach($iconfield as $key => $value)
		{
		   $temp = filterEmptyVar(explode(":",$value));
		   $iconfielddata[$temp[1]] = $temp[0];
		}
		
	    $cur_changeclassname = array();
	    $cur_cachedata = array();
  
		foreach($changeClassName as $key => $value)
		{
			if($value['dir'] == $cachedir)
			{
		        $cur_changeclassname = $value;
			}
		}
        
		if(isset($cur_changeclassname['type']) &&$cur_changeclassname['type']=='tags')
		{
		  
          $cur_cachedata = include(APP_ROOT .'/' .$cur_changeclassname['dir']);	
		  $cur_cachedata = $cur_cachedata['data'];	  
		}else if(isset($cur_changeclassname['type']) &&$cur_changeclassname['type']=='cache')
		{
          $cur_cachedata = include(APP_ROOT .'/' .$cur_changeclassname['dir']);			  
		}else
		{
		  $cur_cachedata = array();
		}	  


		 $cur_select_str = "";		 
		 foreach($cur_cachedata as $key => $value)
		 {  
		    if(is_array($value)&&!empty($value))
			{
			    foreach($value as $key1 => $value1)
				{
				     if(is_array($value1)&&!empty($value1))
					 {
					     foreach($value1 as $key2 => $value2)
				         {
                             $cur_select_str .= '<option  value="'.$key1.'.'.$key2.'">&nbsp;|&nbsp;&nbsp;&nbsp;'.$key1.'.'.$key2.'</option>';					 
				         } 
					 }else
					 {
                       $cur_select_str .= '<option  value="'.$key1.'">&nbsp;|&nbsp;&nbsp;&nbsp;'.$key1.'</option>';					 
					 }
				}
			}
			break;          
	   
		 }
		 
		$temp_str = ''; 
		foreach($cur_cachedata as $key => $value)
		{
			$temp_str .= '['.$key .']'.$value[$cur_changeclassname['showfileds1']] .'=>'.$value[$cur_changeclassname['showfileds2']].'<br/>';					
		}
		$icontabledata ='';		 
        if(isset($cur_changeclassname['name'])&&!empty($cur_changeclassname['name']))
		{
			if($cur_changeclassname['type']=='icon')
			{			
			   $edit_url = '<a href="'.$this->base.'/index.php/tagsedit/icondataedit/?id='.$cur_changeclassname['name'].'&webroot='.$webroot.'" target="_blank"><font color="red">[edit]</font></a>';	
			}
		$icontabledata .= '<div id="div_'.$cur_changeclassname['name'].'"><input type="checkbox" name="fids[]" value="'.$cur_changeclassname['name'].'" checked="" onclick="removeFid(this);">  <font color="#232323"> &gt; '.$cur_changeclassname['alias'].' &gt; '.$temp_str.$edit_url.'</font></div>';					
		}else
		{
		$icontabledata = '';			
		}
	   
	   
		$data['iconcontent'] = '<psp:tags '.$strdata.' />';		
		$data['iconname'] = $iconname;	
			
        $htmlnamearray = array();
        $temparray = array();
        $temparray1 = array();
		
        $temparray = $this->get_all_dirname($webroot1 . DS .'tags');
        $temparray1 = include($webroot1 . DS .'tags' . DS . 'tags.php');
		foreach($temparray1 as $key => $value)
		{

		   $htmlnamearray[$key] = $key.'.html';
		}

	    foreach($temparray as $key => $value)
		{
		   if(strpos($value,'.html'))
		   {
		    $htmlnamearray[str_replace('.html','',$value)] = $value;
		   }
		}
        
        $icondataarray = array();
        $temparray = array();
        $temparray = $this->get_all_dirname($webroot1 . DS .'tags' . DS . 'tempdata');
	    foreach($temparray as $key => $value)
		{
		   if(strpos($value,'.tags')) {
		    $icondataarray[str_replace('.tags','',$value)] = $value;
		   }
		}


		$code= file_get_contents($webroot1 . DS .'tags' . DS . $data['config']['htmlname'].'.html');	
		$relativepath = str_replace(APP_ROOT,'',$webroot1 . DS .'tags');
		
		$relativepath = str_replace(DS,'/',$relativepath);		
        $yuanrelativepath = $relativepath .'/';		
        $bianrelativepath = str_replace('/','\/',$yuanrelativepath);		
			
        $this->set('yuanrelativepath', $yuanrelativepath);		
        $this->set('bianrelativepath', $bianrelativepath);
		
        $this->set('relativepath', $relativepath);
		
        $this->set('icontabledata', $icontabledata);
	
        $this->set('code', $code);
        $this->set('htmlname', $data['config']['htmlname']);		

        $this->set('changeClassName', $changeClassName);

        $this->set('icondataarray', $icondataarray);
        $this->set('htmlnamearray', $htmlnamearray);
        $this->set('cachearray', $cachearray);

        $this->set('cur_select_str', $cur_select_str);

		     
        $this->set('viewdata', $this->get_all_dirname($webroot1));

	    $this->set('iconfielddata', $iconfielddata);
		

	    $this->set('data', $data);
	    $this->set('weburl', $weburl);
	    $this->set('fromurl', $fromurl);	
						
	    $this->set('urlstr', $urlstr);
		
	    $this->set('compeld', $compeld);
	    $this->set('webroot', $webroot);
	    $this->set('app', $app);
	    $this->set('act', $act);
	
        $this->set('pagename', $pagename);			
        $this->set('strtempdata', $strtempdata);		    

    }

	

    function viewmanagelabeledit()
	{
        if(!isset(Request::$query['dopost']))Request::$query['dopost']='';
	    $action = Request::$query['dopost'];
	   if($action=='post')
	   {
	   
		    $pagename = $this->_urldecode(Request::$data['pagename']);
		 
		    $strtempdata = Request::$data['strtempdata'];
		 
		    $fromurl = $this->_urldecode(Request::$data['fromurl']); 
		 
		    $compeld = $this->_urldecode(Request::$data['compeld']); 

		    $webroot = $this->_urldecode(Request::$data['webroot']); 
		 


		    $app = Request::$data['app'];		 
		    $act = Request::$data['act'];		   
	   	   
	 
	     $labels = Request::$data['labels'];
		 $labels = str_replace("\t","   &nbsp;  &nbsp;",$labels);
		 $labels = str_replace("\r","",$labels);
		 $labels = str_replace("\n","",$labels);
		 
		 $strdata = $this->filtrate($strtempdata);	 
         $this->icon_to_array($strdata);
	     $old_icondata = $this->icon_to_array;
         $old_icondata['labels'] = $labels;

        $iconcontent='';
		foreach($old_icondata as $key => $value)
		{ 
		   if($key == 'labels') { 
		       $value = str_replace(array('"',"'"),array('"',''),$value);
			   
		   }else {
		       $value = str_replace(array('&quot;','&#39;'),array('"',"'"),$value);
		   }
		   $iconcontent .= '  '.$key."=\'".$value."\'";
		}			 	  	

		$iconcontent = str_replace(array('\\'),array(''),$iconcontent);	

		    $filename = $webroot . DS .$pagename;
		    $savefilename = $compeld;
		    $savedir = str_replace($pagename ,''  ,$savefilename);			
					
		$tempfilename = '';				
		if(is_file($savefilename) && @ filemtime($savefilename) > @ filemtime($filename))
		{			
		     $tempfilename = $savefilename;				   			
		}
		else
		{
		     $tempfilename = $filename;					
		}
				
		$fileContent = "";
		if (!($fileContent = file_get_contents($tempfilename))) 
		{
				return false;
		}
				
		$fileContent = preg_replace('/^(\xef\xbb\xbf)/', '', $fileContent); //EFBBBF
		$fileContent = str_replace($strdata, $iconcontent, $fileContent);
		
		if (!file_exists($savefilename)) {
                $CviewFileName = str_replace(array('\\','/'), DS ,$savefilename);
				$temp = explode(DS, $CviewFileName);
				array_pop( $temp );
                $tempFileDir = implode( DS.'' , $temp );
				get_dir($tempFileDir);
		}
		
		if (!file_put_contents($savefilename,  $fileContent)) {
			return false;
		}
		echo '<script >alert("修改成功!");parent.location.reload();</script>';
		exit;
/* 		$notice = &get_message(array('msg'=>'修改成功','url'=>$this->_urldecode($fromurl),'title'=>'Notice'));
		$notice ->display(); */	
   
	   }
		
		
 		$iconname = Request::$query['iconname'];
		
		$pagename = Request::$query['pagename'];	
			
		$strdata = Request::$query['data'];
		
		$weburl = Request::$query['weburl'];
		
		$webroot = Request::$query['webroot'];
		
		$act = Request::$query['act'];	
		
		$app = Request::$query['app'];
		
		$compeld = Request::$query['compeld'];		
		
		$compeld= $this->_urldecode($compeld);
		$compeld1 = $compeld;				
		$compeld= $this->_urlencode($compeld);


		$pagename= $this->_urldecode($pagename);
		$pagename1 = $pagename;				
		$pagename= $this->_urlencode($pagename);

		$webroot= $this->_urldecode($webroot);
		$webroot1 = $webroot;				
		$webroot= $this->_urlencode($webroot);		
	
		$weburl= $this->_urldecode($weburl);
		$fromurl = $weburl;				
		$weburl= $this->_urlencode($weburl);
		
		$divwidth = Request::$query['divwidth'];
				
		$divheight = Request::$query['divheight'];
		
		$strtempdata = $strdata;
		$urlstr="&weburl={$weburl}&compeld={$compeld}&app={$app}&act={$act}&webroot={$webroot}&iconname={$iconname}&pagename={$pagename}&data={$strdata}&divwidth={$divwidth}&divheight={$divheight}";

		$strdata = $this->filtrate($strdata);	

		$this->icon_to_array($strdata);
		
	    $iconconfig = $this->icon_to_array;

	    $this->set('data', $iconconfig);

	    $this->set('iconname', $iconname);

	    $this->set('weburl', $weburl);
	    $this->set('fromurl', $fromurl);	
							
	    $this->set('urlstr', $urlstr);
	
		
	    $this->set('compeld', $compeld);
	    $this->set('webroot', $webroot);
	    $this->set('app', $app);
	    $this->set('act', $act);
	
        $this->set('pagename', $pagename);			
        $this->set('strtempdata', $strtempdata);		
		
		
		
	}	

	
	
    function viewmanageadplaceedit()
    {	
	    $dopost = Request::get_args('dopost');   
	   
	    if($dopost=='post')
	    {
		    $pagename = $this->_urldecode(Request::$data['pagename']);
		 
		    $strtempdata = Request::$data['strtempdata'];
		 
		    $fromurl = $this->_urldecode(Request::$data['fromurl']); 
		 
		    $compeld = $this->_urldecode(Request::$data['compeld']); 

		    $webroot = $this->_urldecode(Request::$data['webroot']); 
		 


		    $app = Request::$data['app'];		 
		    $act = Request::$data['act'];			
   
	 
	        $tplpart_1 = Request::$data['tplpart_1'];		 

	        $mokuai = Request::$data['mokuai'];
	        $filename = Request::$data['filename'];

		 
		    $strdata = $this->filtrate($strtempdata);	 
            $this->icon_to_array($strdata);
	        $old_icondata = $this->icon_to_array;


		 	
            if($mokuai==1)
		   {
  		        $temp_html_name = filterEmptyVar(explode(".",$old_icondata['htmlname']));
		        $old_icondata['htmlname'] = $temp_html_name[0];			
		        $icon_dir = $webroot . DS . 'tags';
			    if($filename)
			    {
		            $iconfile = $filename.'.html';
		            $iconfilename = $filename.'';
		            $old_icondata['htmlname'] = $iconfilename;					
			    }else{
		            $iconfile = $old_icondata['htmlname'].'.'.time().'.html';
		            $iconfilename = $old_icondata['htmlname'].'.'.time().'';
		            $old_icondata['htmlname'] = $iconfilename;					
			    }
		
	            $temparray = $this->get_all_dirname($icon_dir);
                $is = 1;
	            foreach($temparray as $key => $value)
		        {
		            if($value==$iconfile)
		            {
                        $is = 0;break;
		            }	
                }
			
		        if($is)
		        {
                    $iconhtml = stripslashes($tplpart_1);
		            $iconhtml = str_replace(array('&quot;','&#39;'),array('"',"'"),$iconhtml);				
                    $iconhtmlarray = @include($icon_dir . DS .'tags.php');
		            $iconhtmlarray[$iconfilename]['name'] =$iconfilename;
                    $iconhtmlarray[$iconfilename]['html'] =$iconhtml;
				    $iconhtmlarray[$iconfilename]['config'] =array();
					if (!(file_put_contents($icon_dir . DS .'tags.php', "<?php \nreturn " . var_export($iconhtmlarray , true) . ";\n?>")
					     && file_put_contents($icon_dir.DS.$iconfile,$iconhtml))
						) {
			            return false;
		            }				
		
		        } else {		
		            echo '<script >alert("Icondata名称有相同，请重新提交!");parent.location.reload();</script>';
		            exit;					
		        }
		     
		    }else if ($mokuai==0) {

		        $icon_dir = $webroot . DS . 'tags';
		        $iconfile = $old_icondata['htmlname'].'.html';
		        $iconfilename = $old_icondata['htmlname'];		 
                $iconhtml = stripslashes($tplpart_1);
		        $iconhtml = str_replace(array('&quot;','&#39;'),array('"',"'"),$iconhtml);				
                $iconhtmlarray = include($icon_dir . DS .'tags.php');
		        $iconhtmlarray[$iconfilename]['name'] =$iconfilename;
                $iconhtmlarray[$iconfilename]['html'] =$iconhtml;
				$iconhtmlarray[$iconfilename]['config'] =array();
				
				if (!(file_put_contents($icon_dir . DS .'tags.php', "<?php \nreturn " . var_export($iconhtmlarray , true) . ";\n?>")
					     && file_put_contents($icon_dir.DS.$iconfile,$iconhtml))
				) {
			        return false;
		        }				
		 
		    }

            $iconcontent = '';
		    foreach($old_icondata as $key => $value)
		    {
		        $iconcontent .= '  '.$key."=\'".$value."\'";
		    }			 	  	

		    $iconcontent = str_replace(array('&quot;','&#39;','\\'),array('"',"'",''),$iconcontent);			

		    $filename = $webroot . DS .$pagename;
		    $savefilename = $compeld;
		    $savedir = str_replace($pagename ,''  ,$savefilename);			
			

		    $tempfilename = '';				
		    if(is_file($savefilename) && @ filemtime($savefilename) > @ filemtime($filename))
		    {			
		        $tempfilename = $savefilename;				   			
		    } else	{
		        $tempfilename = $filename;					
		    }		
		    $fileContent = "";
		    if (!($fileContent = file_get_contents($tempfilename))) 
		    {
				return false;
		    }
				
		    $fileContent = preg_replace('/^(\xef\xbb\xbf)/', '', $fileContent); //EFBBBF
		    $fileContent = str_replace($strdata, $iconcontent, $fileContent);
		
		    if (!file_exists($savefilename)) {
                $CviewFileName = str_replace(array('\\','/'), DS ,$savefilename);
				$temp = explode(DS, $CviewFileName);
				array_pop( $temp );
                $tempFileDir = implode( DS.'' , $temp );
				get_dir($tempFileDir);
		    }
		
		    if (!file_put_contents($savefilename,  $fileContent)) {
			    return false;
		    }
		    echo '<script >alert("修改成功!");parent.location.reload();</script>';
		    exit;
/* 		     $notice = &get_message(array('msg'=>'修改成功','url'=>$this->_urldecode($fromurl),'title'=>'Notice'));
		    $notice ->display();	 */
   
	    }
       if($dopost=="getadplace")
	   {
	   
            header('Content-type: text/html; charset='.Param::read('App.encoding'));	   
			$datype =array(
				"word"=>"文字广告",
				"pic"=>"图片广告",
				"swf"=>"FLASH广告",
				"code"=>"代码广告",
				"duilian"=>"对联广告"
				);
			$page = Request::get_args('page');	
			$url_this = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];			
			$this->loadModel('adplace');			
			$tb=array('table' => " `".$this->adplace->tablePrefix."ad_place` as r ",
				'fields' =>" * ",
				'conditions' => ' 1 ',
				'order' => '',
				'limit' => '10', 'url' => preg_replace("/&page=([^&]+)/","",$url_this),
				'page' => $page,'plist' => 6,
				'startPage' => true,'DbConfig' => null
				) ; 
			
			$result =  $this->fetch_page($tb);	
			$str ='';
			
            foreach($result['data']  as $value) {
				if( $value["ifsale"] ){
					$value["ifsale"] ='<font color="red">允许购买</font>';
				}else{
					$value["ifsale"] ="禁止购买";
				}				
				$str .='<tr >
					<td  align="center">
						<input type="hidden" name="hiddenField"  id="adplace_'.$value['id'].'" value="<!--广告开始--><SCRIPT LANGUAGE=\'JavaScript\' src=\''.Router::url('psptool/ad/').'?app=ad&act=js&adid='.$value['keywords'].'\'></SCRIPT><!--广告结束-->">				
						<a href="javascript::" onClick="padplacecha(\''.$value['id'].'\');" style="color: #4380C0;font-size: 14px;font-weight: bold;text-decoration: none;">点击插入</a>	
					</td>
					<td  align="center">'.$value['name'].'</td>
					<td  align="center">'.$datype[$value['type']].'</a></td>
					<td  align="center">'.$value['ifsale'].'</td>        
					<td  align="center">'.$value['hits'].'</td>
					<td  align="center">
						<a href="'.$value['demourl'].'" target="_blank" onclick="if(\''.$value['demourl'].'\'==\'\')alert(\'本广告位没有设置预览网址\');return false;" alt=\'点击预览\'>预览</a>&nbsp; 
					</td>
				</tr>';		
			}
		    if(!empty($str)) {	   
				$str .='<tr>
					<td colspan="6" align="right">
					<br/>
								<a href="'.$result['page']['head'].'">首页</a> 
								<a href="'.$result['page']['prevPage'].'">上一页</a> 
								<a href="'.$result['page']['nextPage'].'">下一页 </a> 
								<a href="'.$result['page']['foot'].'">尾页</a> 
								当前第'.$result['page']['current']['num'].'页/共'.$result['page']['pageCount'].'页		
					</td>
				</tr>';		   
		    }
			$str = '			
			<body style="background-color:#fff; text-align:center;">
			<table style="width:100%;">
			<thead>
			<tr><th >插入广告</th><th>广告位名称</th><th>广告类型</th><th>申请购买</th><th>点击</th><th>操作</th></tr>
			</thead><tbody>'.$str.'</tbody></table>';	
			$str .= '
			<script>			
				function padplacecha(Objid){
					var va=document.getElementById("adplace_"+Objid).value;
					parent.adplacecha(va);
				}
			</script></body>';	
			echo $str;
	       // echo '<script>parent.inputcontent(\''.$str.'\');</script>';
	        exit;			
        }

		
 		$iconname = Request::$query['iconname'];
		
		$pagename = Request::$query['pagename'];	
			
		$strdata = Request::$query['data'];
		
		$weburl = Request::$query['weburl'];
		
		$webroot = Request::$query['webroot'];
		
		$act = Request::$query['act'];	
		
		$app = Request::$query['app'];
		
		$compeld = Request::$query['compeld'];		
		
		$compeld= $this->_urldecode($compeld);
		$compeld1 = $compeld;				
		$compeld= $this->_urlencode($compeld);


		$pagename= $this->_urldecode($pagename);
		$pagename1 = $pagename;				
		$pagename= $this->_urlencode($pagename);

		$webroot= $this->_urldecode($webroot);
		$webroot1 = $webroot;				
		$webroot= $this->_urlencode($webroot);		
	
		$weburl= $this->_urldecode($weburl);
		$fromurl = $weburl;				
		$weburl= $this->_urlencode($weburl);
		
		$divwidth = Request::$query['divwidth'];
				
		$divheight = Request::$query['divheight'];
		
		$strtempdata = $strdata;
		$urlstr="&weburl={$weburl}&compeld={$compeld}&app={$app}&act={$act}&webroot={$webroot}&iconname={$iconname}&pagename={$pagename}&data={$strdata}&divwidth={$divwidth}&divheight={$divheight}";

		$strdata = $this->filtrate($strdata);	

		$this->icon_to_array($strdata);
	    $data['config'] = $this->icon_to_array;
		if(!isset($data['config']['fields'])){
		    $data['config']['fields'] = '';
		}
		if(!isset($data['config']['limit'])){
		    $data['config']['limit'] = '';
		}		
        $iconfield =  filterEmptyVar(explode("|",$data['config']['fields']));
		$iconfielddata = array();
        foreach($iconfield as $key => $value)
		{
		   $temp = filterEmptyVar(explode(":",$value));
		   $iconfielddata[$temp[1]] = $temp[0];
		}
        $limitnum = filterEmptyVar(explode(",",$data['config']['limit']));

		if(empty($limitnum[1]))$limitnum[1]=10;
	    $limitnum = $limitnum[1];
	   
		$data['iconcontent'] = '<psp:tags '.$strdata.' />';
		$data['iconname'] = $iconname;	
			


		$code= file_get_contents($webroot1 . DS .'tags' . DS . $data['config']['htmlname'].'.html');	 
	
		$code = preg_replace('/^(\xef\xbb\xbf)/', '', $code); //EFBBBF

		
	
        $this->set('code', $code);
        $this->set('htmlname', $data['config']['htmlname']);		


		
	    $this->set('weburl', $weburl);
	    $this->set('fromurl', $fromurl);	
						
	    $this->set('urlstr', $urlstr);
		
	    
	    $this->set('compeld', $compeld);
	    $this->set('webroot', $webroot);
	    $this->set('app', $app);
	    $this->set('act', $act);
	
        $this->set('pagename', $pagename);			
        $this->set('strtempdata', $strtempdata);	
    }	
	
	
	
	
	

    function icondatalist()
    {		
		$webroot = Request::$query['webroot'];		
		$webroot= $this->_urldecode($webroot);
		$webroot1 = $webroot;		
        $icondataarray = array();
        $temparray = array();
		$icon_dir = $webroot1 . DS .'tags' . DS . 'tempdata';
        $temparray = $this->get_all_dirname($icon_dir);
	    foreach($temparray as $key => $value)
		{
		    if(strpos($value,'.tags')) {
				$icondataarray[$value]['name'] = str_replace('.tags','',$value);
				$icondataarray[$value]['dir'] = $value;
				$temp_data = include($icon_dir.'/'.$value);
				$icondataarray[$value]['data'] =  "<?php \nreturn " . var_export($temp_data['data'] , true) . ";\n?>";
				$icondataarray[$value]['description'] = $temp_data['description'] ;					
		    }
		}
        $this->set('data', $icondataarray);
	    $this->set('webroot', $this->_urlencode($webroot));
		
 		$iconname = Request::$query['iconname'];
		
		$pagename = Request::$query['pagename'];	
			
		$strdata = Request::$query['data'];
		
		$weburl = Request::$query['weburl'];
		
		$webroot = Request::$query['webroot'];
		
		$act = Request::$query['act'];	
		
		$app = Request::$query['app'];
		
		$compeld = Request::$query['compeld'];		
		
		$compeld= $this->_urldecode($compeld);
		$compeld1 = $compeld;				
		$compeld= $this->_urlencode($compeld);


		$pagename= $this->_urldecode($pagename);
		$pagename1 = $pagename;				
		$pagename= $this->_urlencode($pagename);

		$webroot= $this->_urldecode($webroot);
		$webroot1 = $webroot;				
		$webroot= $this->_urlencode($webroot);		
	
		$weburl= $this->_urldecode($weburl);
		$fromurl = $weburl;				
		$weburl= $this->_urlencode($weburl);
		
		$divwidth = Request::$query['divwidth'];
				
		$divheight = Request::$query['divheight'];
		
		$strtempdata = $strdata;
		$urlstr="&weburl={$weburl}&compeld={$compeld}&app={$app}&act={$act}&webroot={$webroot}&iconname={$iconname}&pagename={$pagename}&data={$strdata}&divwidth={$divwidth}&divheight={$divheight}";
	    $this->set('urlstr', $urlstr);		
		
		
		
		
    }

	
    function icondataadd()
    {	
		$webroot = Request::$query['webroot'];		
	    if(!isset($_GET['type']))
		{
		$_GET['type'] = '';
		}
        $type = $_GET['type'];
        $this->set('idtime', time());
        $this->set('type', $type);
	    $this->set('webroot', $this->_urlencode($webroot));


 		$iconname = Request::$query['iconname'];
		
		$pagename = Request::$query['pagename'];	
			
		$strdata = Request::$query['data'];
		
		$weburl = Request::$query['weburl'];
		
		$webroot = Request::$query['webroot'];
		
		$act = Request::$query['act'];	
		
		$app = Request::$query['app'];
		
		$compeld = Request::$query['compeld'];		
		
		$compeld= $this->_urldecode($compeld);
		$compeld1 = $compeld;				
		$compeld= $this->_urlencode($compeld);


		$pagename= $this->_urldecode($pagename);
		$pagename1 = $pagename;				
		$pagename= $this->_urlencode($pagename);

		$webroot= $this->_urldecode($webroot);
		$webroot1 = $webroot;				
		$webroot= $this->_urlencode($webroot);		
	
		$weburl= $this->_urldecode($weburl);
		$fromurl = $weburl;				
		$weburl= $this->_urlencode($weburl);
		
		$divwidth = Request::$query['divwidth'];
				
		$divheight = Request::$query['divheight'];
		
		$strtempdata = $strdata;
		$urlstr="&weburl={$weburl}&compeld={$compeld}&app={$app}&act={$act}&webroot={$webroot}&iconname={$iconname}&pagename={$pagename}&data={$strdata}&divwidth={$divwidth}&divheight={$divheight}";
	    $this->set('urlstr', $urlstr);	
		
    }	
	

    function icondataedit()
    {	
		$id= $_GET['id'];		
		$webroot = Request::$query['webroot'];
		$webroot= $this->_urldecode($webroot);
		$webroot1 = $webroot;		
		$filedir = $webroot1 . DS .'tags' . DS . 'tempdata/'.$id.'.tags';		
        $icondata = include($filedir);
        $type = $icondata['type'];
	    $data = $icondata['data'];
	    $description = $icondata['description'];
		$idtime ='';
		if($type =='file')
		{
            foreach($data as $key =>$value)
		    {
				if(isset($value['upfile']))
				{
				   $temp = explode('/', $value['upfile']); 
				   if(isset($temp[1])&&!empty($temp[1]))
					{
					  $idtime = $temp[1];
					  break;	
					}
					
				}
		    }
        }
		
        $this->set('idtime', $idtime);
        $this->set('id', $id);
        $this->set('type', $type);
        $this->set('data', $data);
        $this->set('description', $description);
	    $this->set('webroot', $this->_urlencode($webroot));	


 		$iconname = Request::$query['iconname'];
		
		$pagename = Request::$query['pagename'];	
			
		$strdata = Request::$query['data'];
		
		$weburl = Request::$query['weburl'];
		
		$webroot = Request::$query['webroot'];
		
		$act = Request::$query['act'];	
		
		$app = Request::$query['app'];
		
		$compeld = Request::$query['compeld'];		
		
		$compeld= $this->_urldecode($compeld);
		$compeld1 = $compeld;				
		$compeld= $this->_urlencode($compeld);


		$pagename= $this->_urldecode($pagename);
		$pagename1 = $pagename;				
		$pagename= $this->_urlencode($pagename);

		$webroot= $this->_urldecode($webroot);
		$webroot1 = $webroot;				
		$webroot= $this->_urlencode($webroot);		
	
		$weburl= $this->_urldecode($weburl);
		$fromurl = $weburl;				
		$weburl= $this->_urlencode($weburl);
		
		$divwidth = Request::$query['divwidth'];
				
		$divheight = Request::$query['divheight'];
		
		$strtempdata = $strdata;
		$urlstr="&weburl={$weburl}&compeld={$compeld}&app={$app}&act={$act}&webroot={$webroot}&iconname={$iconname}&pagename={$pagename}&data={$strdata}&divwidth={$divwidth}&divheight={$divheight}";
	    $this->set('urlstr', $urlstr);			

    }	
	

/*
icondata的数据类型有file文件类型，data数据类型
*/
    function icondatadel()
    {
		$id= $_GET['id'];	
		$webroot = Request::$query['webroot'];
		$webroot= $this->_urldecode($webroot);
		$webroot1 = $webroot;		
		$filedir = $webroot1 . DS .'tags' . DS . 'tempdata/'.$id.'.tags';		
	    if(!is_dir($filedir)&&file_exists($filedir)){
		   del_file($filedir);	
	    }		
        $this->flash("删除成功", Request::referer());	

    }	
	
	

    function icondataaddpost()
    {
        $data = $_POST['data'];
        $type = $_POST['type'];
        $name = $_POST['name'];
        $description = $_POST['description'];
		$webroot = $_POST['webroot'];
		$webroot= $this->_urldecode($webroot);	
		$webroot1 = $webroot;		
		$icon_dir = $webroot1 . DS .'tags' . DS . 'tempdata/';		
        $icondataarray = array();		
        $temparray = $this->get_all_dirname($icon_dir);
        $is = 1;
	    foreach($temparray as $key => $value){
		    if($value==$name.'.tags'){
                $is = 0;break;
		    }
		}
		if($is)	{		
			$temp_array = array('type'=>$type,'data'=>$data,'description'=>$description);
			$cache_filename = $icon_dir.$name.'.tags';		
			write_file($cache_filename, "<?php \nreturn " . var_export($temp_array , true) . ";\n?>");
            $this->flash("操作成功!", Request::referer());				
		}else{	
            $this->flash("Icondata名称有相同，请重新输入", Request::referer());		
		}         
    }



    function icondataeditpost() {
		$id = $_POST['id'];
        $data = $_POST['data'];
        $type = $_POST['type'];
        $name = $_POST['name'];
        $description = $_POST['description'];
		$webroot = $_POST['webroot'];
		$webroot= $this->_urldecode($webroot);
		$webroot1 = $webroot;	
		$icon_dir = $webroot1 . DS .'tags' . DS . 'tempdata/';		

        $is = 1;
        if($id != $name){
			$icondataarray = array();		
			$temparray = $this->get_all_dirname($icon_dir);		

	        foreach($temparray as $key => $value) {
		        if($value==$name.'.tags'){
                    $is = 0;break;
		        }
		    }
		    if($is){
			    if(!is_dir($icon_dir.$id.'.tags')&&file_exists($icon_dir.$id.'.tags')){
		           del_file($icon_dir.$id.'.tags');	
		        }	   
		    }
		}
		
		if($is){		
		    $temp_array = array('type'=>$type,'data'=>$data,'description'=>$description);
		    $cache_filename = $icon_dir.$name.'.tags';	
	        write_file($cache_filename, "<?php \nreturn " . var_export($temp_array , true) . ";\n?>");
            $this->flash("操作成功!", Request::referer());
		}else{
            $this->flash("Icondata名称有相同，请重新输入", Request::referer());		
		}         
    }	
	
/*
<psp:filedlist>
goods => goods.id:icon1|goods.goods_name:icon2| ;
shop_info => shop_info.id:icon1|shop_info.shop_name:icon2|;
</psp:filedlist>处理这里面的字符
*/
	function _filed_list_code($str = '') {
		$str= str_replace(array("\r","\n"),array('',''),$str );
		$temp = filterEmptyVar(explode(";",$str));
		$temp1 = array();
        foreach($temp as $key => $value) {
            $temp2 = filterEmptyVar(explode("=>",$value)) ;
			$temp1[trim($temp2[0])] = trim($temp2[1]);			
        }		
	    $this->_filedlistcode = $temp1 ;
	}		
	
	
	function TagsModel($tb = array()) {

        $tb = $tb + array('table' => '', 'fields' => '*',
					'conditions' => '',	'order' => '',
					'limit' => '', 'url' => '',
					'page' => 1,'plist' => 6,
					'startPage' => true,'DbConfig' => null
					) ; 
        extract($tb, EXTR_SKIP);
		if(empty($table)) return array('data'=>array());
	    if($DbConfig ===null) {
		    $DbConfig = 'default';
		}		
        ($conditions=='')?$conditions = '':$conditions = '  WHERE  '.$conditions;		
	    $db =& ConnectionManager::getDataSource($DbConfig);
 		$data = array();
		if($startPage) {
            $page = empty($page) ? 1 : intval($page);
			$plist = empty($plist) ? 6 : intval($plist);			
		    $data1 = $db->fetchRow("SELECT COUNT(*) AS count  FROM {$table} {$conditions} ");
		    $totalnum = $data1[0]['count'];		
	        $totalpage=intval(@ceil($totalnum/$limit));
	        $nextpage=$page+1;
	        $uppage=$page-1;		 
	        if($nextpage>$totalpage){
		        $nextpage=$totalpage;
	        }		 
	        if($uppage<1){
		        $uppage=1;
	        }
	        $s=$page-3;
	        if($s<1){
		       $s=1;
	        }
	        $b=$s + $plist;
	        if($b>$totalpage){
		        $b=$totalpage;
	        }			
	        for($j=$s;$j<=$b;$j++){
		        if($j==$page) {
			        $data['page']['current']['num'] = $j;
					$data['page']['current']['url'] = "$url&page=$j";
		        } else {      
			        $data['page']['plist'][$j] = "$url&page=$j";			         
		        }
	        }		  
            if(!isset($data['page']['current'])){
		        $data['page']['current']['num'] =1;
		        $data['page']['current']['url'] = "$url&page=1"; 
		    }	  
		    $start = ($page-1) * $limit;		  	  
		    $data['page']['head'] =  "$url&page=1";
		    $data['page']['prevPage'] = "$url&page=$uppage";
		    $data['page']['nextPage'] = "$url&page=$nextpage";
		    $data['page']['foot'] = "$url&page=$totalpage";
		    $data['page']['pageCount'] = $totalpage;
		    $data['page']['total'] = $totalnum;
		}else {
		    $start = 0;
		}	
        // 排序(ORDER BY) desc表示倒序,asc表示正序(这个是默认的) 
        $order && $order = ' ORDER BY ' . $order . ' ';
        $limit && $limit = " LIMIT {$start},{$limit} ";
		$query = "SELECT {$fields} FROM {$table} {$conditions} {$order} {$limit}";
		$data['data'] = $db->QueryAll($query);
		return $data;
	}		

	
	function icon_to_array($str = '')
	{
	    if(empty($str))
		{
		 return '';
		}
		if ($this->_psptags == null) 
		{

		    Loader::import('pspframework.utils.tags');
		    $this->_psptags = new PspTags();			

		}	

	    $this->icon_to_array = filterEmptyVar($this->_psptags->strAttribute($str));
	
		foreach($this->icon_to_array as $key=>$value)
		{
		  $this->icon_to_array[$key] = str_replace(array("\'",'\"'),array('',''),$value);

		}
	
		return 1;
	}	
	//得到某目录下的所有文件夹名及文件名
	function get_all_dirname($dirname) {
		$all_dir = array ();
		$lang_dir_handle = opendir($dirname);

		while (($lang_dir = readdir($lang_dir_handle)) !== false) {
			if (!preg_match("/^\./", $lang_dir)) {
				$all_dir[] = $lang_dir;
			}
		}

		closedir($lang_dir_handle);
		return $all_dir;
	}	
	
 	public function _urldecode($msg){
		$msg = urldecode($msg);		
		$msg = str_replace(';gang;','-',$msg);	
		return $msg;
	}
	public function _urlencode($msg){
		$msg = str_replace('-',';gang;',$msg);
		$msg = urlencode($msg);		
		return $msg;
	}
	public function filtrate($msg){
		$msg = str_replace(';deng;','=',$msg);
		$msg = str_replace(';kong;',' ',$msg);
		$msg = str_replace(';shuang;','"',$msg);
		$msg = str_replace(';dang;',"'",$msg);
		$msg = str_replace(';and;',"&&",$msg);		
		$msg = str_replace(';or;',"||",$msg);
		$msg = str_replace(';gang;',"-",$msg);				
		return $msg;
	} 	
}
/**
*读文件函数
**/
function read_file($filename,$method="rb")
{
    $filedata='';
	if($handle=@fopen($filename,$method))
	{
		@flock($handle,LOCK_SH);
		$filedata=@fread($handle,@filesize($filename));
		@fclose($handle);
	}
	return $filedata;
}

/**
*写文件函数
**/
function write_file($filename,$data,$method="rb+",$iflock=1)
{
	@touch($filename);
	$handle=@fopen($filename,$method);
	if($iflock)
	{
		@flock($handle,LOCK_EX);
	}
	@fputs($handle,$data);
	
	if($method=="rb+")
	{
	    @ftruncate($handle,strlen($data));
	}
	
	@fclose($handle);
	@chmod($filename,0777);	
	if( is_writable($filename) )
	{
		return 1;
	}else
	{
		return 0;
	}
}

/**
*删除文件,值不为空，则返回不能删除的文件名
**/
function del_file($path)
{
    $show ="";
	if (file_exists($path))
	{
		if(is_file($path))
		{
			if(	!@unlink($path)	)
			{
				$show.="$path,";
			}
		} 
		else
		{
			$handle = opendir($path);
			while (($file = readdir($handle))!='') 
			{
				if (($file!=".") && ($file!="..") && ($file!=""))
				{
					if (is_dir("$path/$file"))
					{
						$show.=del_file("$path/$file");
					} 
					else
					{
						if( !@unlink("$path/$file") )
						{
							$show.="$path/$file,";
						}
					}
				}
			}
			closedir($handle);
			if(!@rmdir($path))
			{
				$show.="$path,";
			}
		}
	}
	return $show;
}

?>